AmitDiwan has Published 10744 Articles

How to update a field with a particular value if it is null in MySQL?

AmitDiwan

AmitDiwan

Updated on 24-Sep-2019 13:10:08

1K+ Views

To update a field if it is null, use IS NULL property along with the UPDATE command. Let us first create a table −mysql> create table DemoTable (    StudentScore int ); Query OK, 0 rows affected (0.47 sec)Insert some records in the table using insert command −mysql> insert into ... Read More

Insert values from the first table to the second table using two SELECT statements in a single MySQL query

AmitDiwan

AmitDiwan

Updated on 24-Sep-2019 13:08:03

303 Views

To insert values from the first table to another table using two SELECT statements, use SUBQUERY. This will allow you to use only a single MySQL query to get the result in the second table. Let us first create a table −mysql> create table DemoTable1 (    Name varchar(100),   ... Read More

How to ORDER BY FIELD with GROUP BY in a single MySQL query?

AmitDiwan

AmitDiwan

Updated on 24-Sep-2019 13:05:27

316 Views

For this, let us first create a table −mysql> create table DemoTable (    Message text ); Query OK, 0 rows affected (1.15 sec)Insert some records in the table using insert command −mysql> insert into DemoTable values('Good'); Query OK, 1 row affected (0.43 sec) mysql> insert into DemoTable values('Bye'); Query ... Read More

How to set MySQL default value NONE?

AmitDiwan

AmitDiwan

Updated on 24-Sep-2019 13:03:19

781 Views

To set the default value in MySQL, you need to use the DEFAULT keyword. Let us first create a table −mysql> create table DemoTable (    ClientCountryName varchar(100) DEFAULT 'NONE' ); Query OK, 0 rows affected (0.65 sec)We have set DEFAULT above for values not entered while insertion. Now, let ... Read More

MySQL select statement to fetch 5 characters from the left of the string

AmitDiwan

AmitDiwan

Updated on 24-Sep-2019 13:01:07

257 Views

To fetch number of characters from the left of the string, use the LEFT method in MySQL. Let us first create a table −mysql> create table DemoTable (    Name varchar(100) ); Query OK, 0 rows affected (6.58 sec)Insert some records in the table using insert command −mysql> insert into ... Read More

Can we use SELECT NULL statement in a MySQL query?

AmitDiwan

AmitDiwan

Updated on 24-Sep-2019 12:56:07

255 Views

Yes, we can use the SELECT NULL statement in a MySQL query. Let us first create a table −mysql> create table DemoTable (    Name varchar(100) ); Query OK, 0 rows affected (0.49 sec)Insert some records in the table using insert command −mysql> insert into DemoTable values('Chris'); Query OK, 1 ... Read More

Update only a single value from a MySQL table where select from same table ordered in descending order?

AmitDiwan

AmitDiwan

Updated on 24-Sep-2019 12:53:55

101 Views

For this, use ORDER BY DESC with the LIMIT clause. The ORDER BY DESC order in descending order where LIMIT sets the number of records you want. Here, we will set LIMIT 1 since we want only a single record. Let us first create a table −mysql> create table DemoTable ... Read More

How to update a MySQL column by subtracting a value with some conditions?

AmitDiwan

AmitDiwan

Updated on 24-Sep-2019 12:49:00

927 Views

Let us first create a table −mysql> create table DemoTable (    Score int ); Query OK, 0 rows affected (0.65 sec)Insert some records in the table using insert command −mysql> insert into DemoTable values(45); Query OK, 1 row affected (0.11 sec) mysql> insert into DemoTable values(29); Query OK, 1 ... Read More

Return true or false in a MySQL select if another field contains a string?

AmitDiwan

AmitDiwan

Updated on 24-Sep-2019 12:45:24

2K+ Views

To return TRUE or FALSE if another field contains a string, use IF(). Let us first create a tablemysql> create table DemoTable (    FirstName varchar(100),    LastName varchar(100) ); Query OK, 0 rows affected (1.28 sec)Insert some records in the table using insert command −mysql> insert into DemoTable values('Chris', ... Read More

How to get a value more than a particular value from varchar column in MySQL?

AmitDiwan

AmitDiwan

Updated on 24-Sep-2019 12:40:10

474 Views

Since the column wherein you want to get the value more than a particular value is VARCHAR, use the CAST() function. For example, to fetch the value more than 999 from a column with varchar values.Let us first create a table −mysql> create table DemoTable (    Value varchar(100) ); ... Read More

Advertisements