For conditional NOT NULL case, you do not need to use <> and = operator. You need to use IS NULL and IS NOT NULL property because NULL is a special case in MySQL.
To understand the conditional NOT NULL case, let us create a table. The query to create a table is as follows:
mysql> create table ConditionalNotNullDemo -> ( -> Id int NOT NULL AUTO_INCREMENT, -> SendMessage longtext, -> PRIMARY KEY(Id) -> ); Query OK, 0 rows affected (0.62 sec)
Insert some records in the table using insert command. The query is as follows:
mysql> insert into ConditionalNotNullDemo(SendMessage) values(NULL); Query OK, 1 row affected (0.16 sec) mysql> insert into ConditionalNotNullDemo(SendMessage) values('Hi'); Query OK, 1 row affected (0.15 sec) mysql> insert into ConditionalNotNullDemo(SendMessage) values('Hello'); Query OK, 1 row affected (0.20 sec) mysql> insert into ConditionalNotNullDemo(SendMessage) values(NULL); Query OK, 1 row affected (0.18 sec)
Display all records from the table using select statement. The query is as follows:
mysql> select *from ConditionalNotNullDemo;
The following is the output:
+----+-------------+ | Id | SendMessage | +----+-------------+ | 1 | NULL | | 2 | Hi | | 3 | Hello | | 4 | NULL | +----+-------------+ 4 rows in set (0.00 sec)
Here is the demo of IS NULL and IS NOT NULL property.
Case 1: If you want to filter all NULL messages, the use IS NULL property. The query is as follows:
mysql> select *from ConditionalNotNullDemo where SendMessage IS NULL;
The following is the output:
+----+-------------+ | Id | SendMessage | +----+-------------+ | 1 | NULL | | 4 | NULL | +----+-------------+ 2 rows in set (0.00 sec)
Case 2: Use of IS NOT NULL property. If you want to display all valid messages apart from NULL message, you can use IS NULL property. The query is as follows:
mysql> select *from ConditionalNotNullDemo where SendMessage IS NOT NULL;
The following is the output:
+----+-------------+ | Id | SendMessage | +----+-------------+ | 2 | Hi | | 3 | Hello | +----+-------------+ 2 rows in set (0.03 sec)