

- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Conditional NOT NULL case MySQL?
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)
- Related Questions & Answers
- In which conditions, MySQL CASE statement return NULL?
- Working with NULL and IS NOT NULL in MySQL
- CASE WHEN column1 IS NULL THEN NULL ELSE column2 END with MySQL
- Display only NOT NULL values from a column with NULL and NOT NULL records in MySQL
- Difference Between MySql <> NULL and IS NOT NULL?
- Conditional $first in MongoDB aggregation ignoring NULL?
- Removing NOT NULL restriction from column in MySQL?
- Empty string in not-null column in MySQL?
- Enum with NOT NULL in a MySQL field?
- Set 1 for NOT NULL value in MySQL
- Insert default into not null column if value is null in MySQL?
- Sum if all rows are not null else return null in MySQL?
- Check for NULL or NOT NULL values in a column in MySQL
- In MySQL what is the difference between != NULL and IS NOT NULL?
- MySQL CASE statement to place custom values in place of NULL