這是Mysql系列第13篇。html
環境:mysql5.7.25,cmd命令中進行演示。java
當數據的值爲NULL的時候,可能出現各類意想不到的效果,讓人防不勝防,咱們來看看NULL致使的各類神坑,如何避免?mysql
認真看下面的效果sql
mysql> select 1>NULL; +--------+ | 1>NULL | +--------+ | NULL | +--------+ 1 row in set (0.00 sec) mysql> select 1<NULL; +--------+ | 1<NULL | +--------+ | NULL | +--------+ 1 row in set (0.00 sec) mysql> select 1<>NULL; +---------+ | 1<>NULL | +---------+ | NULL | +---------+ 1 row in set (0.00 sec) mysql> select 1>NULL; +--------+ | 1>NULL | +--------+ | NULL | +--------+ 1 row in set (0.00 sec) mysql> select 1<NULL; +--------+ | 1<NULL | +--------+ | NULL | +--------+ 1 row in set (0.00 sec) mysql> select 1>=NULL; +---------+ | 1>=NULL | +---------+ | NULL | +---------+ 1 row in set (0.00 sec) mysql> select 1<=NULL; +---------+ | 1<=NULL | +---------+ | NULL | +---------+ 1 row in set (0.00 sec) mysql> select 1!=NULL; +---------+ | 1!=NULL | +---------+ | NULL | +---------+ 1 row in set (0.00 sec) mysql> select 1<>NULL; +---------+ | 1<>NULL | +---------+ | NULL | +---------+ 1 row in set (0.00 sec) mysql> select NULL=NULL,NULL!=NULL; +-----------+------------+ | NULL=NULL | NULL!=NULL | +-----------+------------+ | NULL | NULL | +-----------+------------+ 1 row in set (0.00 sec) mysql> select 1 in (null),1 not in (null),null in (null),null not in (null); +-------------+-----------------+----------------+--------------------+ | 1 in (null) | 1 not in (null) | null in (null) | null not in (null) | +-------------+-----------------+----------------+--------------------+ | NULL | NULL | NULL | NULL | +-------------+-----------------+----------------+--------------------+ 1 row in set (0.00 sec) mysql> select 1=any(select null),null=any(select null); +--------------------+-----------------------+ | 1=any(select null) | null=any(select null) | +--------------------+-----------------------+ | NULL | NULL | +--------------------+-----------------------+ 1 row in set (0.00 sec) mysql> select 1=all(select null),null=all(select null); +--------------------+-----------------------+ | 1=all(select null) | null=all(select null) | +--------------------+-----------------------+ | NULL | NULL | +--------------------+-----------------------+ 1 row in set (0.00 sec)
結論:任何值和NULL使用運算符(>、<、>=、<=、!=、<>)或者(in、not in、any/some、all)比較時,返回值都爲NULL,NULL做爲布爾值的時候,不爲1也不爲0。微信
mysql> create table test1(a int,b int); Query OK, 0 rows affected (0.01 sec) mysql> insert into test1 values (1,1),(1,null),(null,null); Query OK, 3 rows affected (0.00 sec) Records: 3 Duplicates: 0 Warnings: 0 mysql> select * from test1; +------+------+ | a | b | +------+------+ | 1 | 1 | | 1 | NULL | | NULL | NULL | +------+------+ 3 rows in set (0.00 sec)
上面3條數據,認真看一下,特別是注意上面NULL的記錄。函數
mysql> select * from test1; +------+------+ | a | b | +------+------+ | 1 | 1 | | 1 | NULL | | NULL | NULL | +------+------+ 3 rows in set (0.00 sec) mysql> select * from test1 where a in (null); Empty set (0.00 sec) mysql> select * from test1 where a in (null,1); +------+------+ | a | b | +------+------+ | 1 | 1 | | 1 | NULL | +------+------+ 2 rows in set (0.00 sec)
結論:當IN和NULL比較時,沒法查詢出爲NULL的記錄。spa
mysql> select * from test1 where a not in (1); Empty set (0.00 sec) mysql> select * from test1 where a not in (null); Empty set (0.00 sec) mysql> select * from test1 where a not in (null,2); Empty set (0.00 sec) mysql> select * from test1 where a not in (2); +------+------+ | a | b | +------+------+ | 1 | 1 | | 1 | NULL | +------+------+ 2 rows in set (0.00 sec)
結論:當NOT IN 後面有NULL值時,不論什麼狀況下,整個sql的查詢結果都爲空。3d
mysql> select * from test2; +------+------+ | a | b | +------+------+ | 1 | 1 | | 1 | NULL | | NULL | NULL | +------+------+ 3 rows in set (0.00 sec) mysql> select * from test1 t1 where exists (select * from test2 t2 where t1.a = t2.a); +------+------+ | a | b | +------+------+ | 1 | 1 | | 1 | NULL | +------+------+ 2 rows in set (0.00 sec) mysql> select * from test1 t1 where not exists (select * from test2 t2 where t1.a = t2.a); +------+------+ | a | b | +------+------+ | NULL | NULL | +------+------+ 1 row in set (0.00 sec)
上面咱們複製了表test1建立了表test2。code
查詢語句中使用exists、not exists對比test1.a=test2.a,由於=不能比較NULL,結果和預期一致。htm
mysql> select 1 is not null; +---------------+ | 1 is not null | +---------------+ | 1 | +---------------+ 1 row in set (0.00 sec) mysql> select 1 is null; +-----------+ | 1 is null | +-----------+ | 0 | +-----------+ 1 row in set (0.00 sec) mysql> select null is null; +--------------+ | null is null | +--------------+ | 1 | +--------------+ 1 row in set (0.00 sec) mysql> select null is not null; +------------------+ | null is not null | +------------------+ | 0 | +------------------+ 1 row in set (0.00 sec)
看上面的效果,返回的結果爲1或者0。
結論:判斷是否爲空只能用IS NULL、IS NOT NULL。
mysql> select count(a),count(b),count(*) from test1; +----------+----------+----------+ | count(a) | count(b) | count(*) | +----------+----------+----------+ | 2 | 1 | 3 | +----------+----------+----------+ 1 row in set (0.00 sec)
count(a)返回了2行記錄,a字段爲NULL的沒有統計出來。
count(b)返回了1行記錄,爲NULL的2行記錄沒有統計出來。
count(*)能夠統計全部數據,不論字段的數據是否爲NULL。
mysql> select * from test1 where a is null; +------+------+ | a | b | +------+------+ | NULL | NULL | +------+------+ 1 row in set (0.00 sec) mysql> select count(a) from test1 where a is null; +----------+ | count(a) | +----------+ | 0 | +----------+ 1 row in set (0.00 sec)
上面第1個sql使用is null查詢出告終果,第2個sql中count(a)返回的是0行。
結論:count(字段)沒法統計字段爲NULL的值,count(*)能夠統計值爲null的行。
mysql> create table test3(a int primary key,b int); Query OK, 0 rows affected (0.01 sec) mysql> insert into test3 values (null,1); ERROR 1048 (23000): Column 'a' cannot be null
上面咱們建立了一個表test3
,字段a
未指定不能爲空,插入了一條NULL的數據,報錯緣由:a 字段的值不能爲NULL
,咱們看一下表的建立語句:
mysql> show create table test3; +-------+------------+ | Table | Create Table | +-------+------------+ | test3 | CREATE TABLE `test3` ( `a` int(11) NOT NULL, `b` int(11) DEFAULT NULL, PRIMARY KEY (`a`) ) ENGINE=InnoDB DEFAULT CHARSET=utf8 +-------+------------+ 1 row in set (0.00 sec)
從上面的腳本能夠看出,當字段爲主鍵的時候,字段會自動設置爲not null
。
結論:當字段爲主鍵的時候,字段會自動設置爲not null。
看了上面這些仍是比較暈,NULL的狀況確實比較難以處理,容易出錯,最有效的方法就是避免使用NULL。因此,強烈建議建立字段的時候字段不容許爲NULL,設置一個默認值。
mysql系列大概有20多篇,喜歡的請關注一下,歡迎你們加我微信itsoku或者留言交流mysql相關技術!
原文出處:https://www.cnblogs.com/itsoku123/p/11582628.html