We have seen the SQL SELECT command using the WHERE clause to fetch data from the MySQL table. However, when we try to give the condition to compare the value of the field or column to NULL, it does not work correctly.
To handle this situation, MySQL provides three major operators
is NULL: This operator returns True when the value of the column is null.
is not NULL: operator returns True when the value of the column is not NULL.
<=> operator Comparison value (unlike = operator) is ture, even if two null values
The terms involved are special. You cannot use =null or!=null to find a column with a NULL value. This comparison always tells them whether it is a real failure, because it is impossible. Even the null=null failed.
If you want to find columns that are or are not NULL, use is NULL or is not NULL.
If you want to find a column with a null value, you cannot use the =null test. The following statement does not return any rows because, for any expression,
Expr = null is false:
The code is as follows |
Copy Code |
Mysql> SELECT * from my_table WHERE phone = NULL; |
To find null values, you must use the IS NULL test. The following example shows how to find a null phone number and an empty phone number:
The code is as follows |
Copy Code |
Mysql> SELECT * FROM my_table WHERE phone is NULL; Mysql> SELECT * from my_table WHERE phone = ""; |
To help with null processing, you can use the is null and are not NULL operators and the Ifnull () function.
Example:
Try the following example:
The code is as follows |
Copy Code |
root@host# mysql-u root-p password; Enter password:******* mysql> use tutorials; Database changed mysql> create TABLE Tcount_tbl -> ( -> Tutori Al_author varchar () not NULL, -> tutorial_count INT ->); Query OK, 0 rows affected (0.05 sec) Mysql> INSERT into Tcount_tbl -> (Tutorial_author, Tutorial_count) VALUES (' Mahran ', 20); mysql> INSERT into Tcount_tbl -> (Tutorial_author, Tutorial_count) VALUES (' Mahnaz ', NU LL); mysql> INSERT into Tcount_tbl -> (Tutorial_author, Tutorial_count) VALUES (' Jen ', NULL) ; mysql> INSERT into Tcount_tbl -> (Tutorial_author, Tutorial_count) VALUES (' Gill ', 20); Mysql> SELECT * from TCOUNT_TBL; +-----------------+----------------+ | Tutorial_author | Tutorial_count | +-----------------+----------------+ | Mahran | 20 | | Mahnaz | NULL | | Jen | NULL | | Gill | 20 | +-----------------+----------------+ 4 rows in Set (0.00 sec) Mysql> |
You can see that = and!= do not use null values, as follows:
The code is as follows |
Copy Code |
Mysql> SELECT * from tcount_tbl WHERE tutorial_count = NULL; Empty Set (0.00 sec) Mysql> SELECT * from Tcount_tbl WHERE tutorial_count!= NULL; Empty Set (0.01 sec)
|
To find a record where the Tutorial_count column is or is not NULL, the query should write this:
The code is as follows |
Copy Code |
Mysql> SELECT * from TCOUNT_TBL -> WHERE Tutorial_count is NULL; +-----------------+----------------+ | Tutorial_author | Tutorial_count | +-----------------+----------------+ | Mahnaz | NULL | | Jen | NULL | +-----------------+----------------+ 2 rows in Set (0.00 sec) Mysql> SELECT * from TCOUNT_TBL -> WHERE Tutorial_count is not NULL; +-----------------+----------------+ | Tutorial_author | Tutorial_count | +-----------------+----------------+ | Mahran | 20 | | Gill | 20 | +-----------------+----------------+ 2 rows in Set (0.00 sec) |
subquery not in and NULL in the not EXISTS
In some cases, a subquery of not in form returns an empty result set, but it is returned to normal after overwriting it to the not EXISTS form, as follows:
Build table:
The code is as follows |
Copy Code |
mysql> CREATE TABLE T2 (col1 int default NULL, col2 int default null); Query OK, 0 rows affected (0.01 sec) mysql> CREATE TABLE t3 (col1 int default NULL, col2 int default null); Query OK, 0 rows affected (0.01 sec) |
Add Data:
The code is as follows |
Copy Code |
Mysql> INSERT into T2 VALUES (1,2), (1,3); Query OK, 2 rows Affected (0.00 sec) Records:2 duplicates:0 warnings:0 mysql> INSERT into T3 VALUES (1,2), (1,null); Query OK, 2 rows Affected (0.00 sec) Records:2 duplicates:0 warnings:0 |
Execute the following query:
code is as follows |
copy code |
Mysql> SELECT * from T2 WHERE col2 to (select col2 from T3); Empty set (0.00 SE c) mysql> SELECT * T2 where not EXISTS (select 1 from t3 where t3.col2 = t2.co L2); +------+------+ | col1 | col2 | +------+------+ | 1 | 3 | +------+------+ 1 row in Set (0.00 sec) |
Why is that? This is to say from the specifics of the MySQL database null:
There are three states in MySQL: True, False, Unknown, and any null comparison operations are Unknown states, as follows:
code is as follows |
copy code |
mysql> SELECT 1 = null, 1 <> NULL, 1 < NULL, 1 > null; +----------+-----------+-- --------+----------+ | 1 = null | 1 <> NULL | 1 < NULL | 1 > NULL | & nbsp +----------+-----------+----------+----------+ | NULL | NULL | NULL | NULL | +----------+-----------+----------+----------+ 1 row in Set (0.00 sec) |
And all of the query criteria (on, where, and having) are treated as false with the unknown state.
So the query field in the first query is equivalent to: Col2 not in (2, NULL) => col2 <> 2 and col2 <> NULL => True and Unknow => Unknow ; False
The query condition is never false, so the query does not return a result.
The Not EXISTS is loop-executed
He first executes SELECT 1 from t3 WHERE t3.col2 = 2
Returns the result, the query condition is False after the not EXISTS operation, so no output is made.
Next execute SELECT 1 from t3 WHERE t3.col2 = 3
No return results. If the query condition is True after the not EXISTS operation, the result of this query is output.
Therefore, if a not in subquery does not return results, pay special attention to whether the result set of the inner query contains null values, and if so, try to rewrite the query to not EXISTS form.