Display NULL and NOT NULL records except a single specific value in MySQL


To display NULL records, use IS NULL in MySQL. To ignore a single value, use the != operator , which is an alias of the <> operator.

Let us first create a table −

mysql> create table DemoTable
(
   Id int NOT NULL AUTO_INCREMENT PRIMARY KEY,
   PlayerName varchar(40)
);
Query OK, 0 rows affected (0.50 sec)

Insert some records in the table using insert command −p>

mysql> insert into DemoTable(PlayerName) values('Adam');
Query OK, 1 row affected (0.13 sec)
mysql> insert into DemoTable(PlayerName) values(NULL);
Query OK, 1 row affected (0.14 sec)
mysql> insert into DemoTable(PlayerName) values('Sam');
Query OK, 1 row affected (0.13 sec)
mysql> insert into DemoTable(PlayerName) values('Mike');
Query OK, 1 row affected (0.08 sec)

Display all records from the table using select statement −

mysql> select *from DemoTable;

This will produce the following output −

+----+------------+
| Id | PlayerName |
+----+------------+
|  1 | Adam       |
|  2 | NULL       |
|  3 | Sam        |
|  4 | Mike       |
+----+------------+
4 rows in set (0.00 sec)

Following is the query to display NULL and NOT NULL records, ignoring a single specific record −

mysql> select *from DemoTable
   where PlayerName!='Sam'
   or PlayerName IS NULL;

This will produce the following output −

+----+------------+
| Id | PlayerName |
+----+------------+
|  1 | Adam       |
|  2 | NULL       |
|  4 | Mike       |
+----+------------+
3 rows in set (0.00 sec)
raja
Published on 07-Oct-2019 16:34:16
Advertisements