Perform MySQL SELECT on fields containing null values?

MySQLMySQLi Database

To check for NULL values in SELECT, use MySQL NULL. Let us first create a table −

mysql> create table DemoTable1455
   -> (
   -> Name varchar(20)
   -> );
Query OK, 0 rows affected (0.47 sec)

Insert some records in the table using insert command −

mysql> insert into DemoTable1455 values('John');
Query OK, 1 row affected (0.22 sec)
mysql> insert into DemoTable1455 values(NULL);
Query OK, 1 row affected (0.12 sec)
mysql> insert into DemoTable1455 values('');
Query OK, 1 row affected (0.19 sec)

Display all records from the table using select statement −

mysql> select * from DemoTable1455;

This will produce the following output −

+------+
| Name |
+------+
| John |
| NULL |
|      |
+------+
3 rows in set (0.00 sec)

Following is the query to perform MySQL select on fields containing null values −

mysql> select * from DemoTable1455 where Name <> 'John' or Name is null;

This will produce the following output −

+------+
| Name |
+------+
| NULL |
|      |
+------+
2 rows in set (0.00 sec)
raja
Published on 10-Dec-2019 06:17:00
Advertisements