- 任何值和NULL使用运算符(>、<、>=、<=、!=、<>)或者(in、not in、any/
some、all)比较时,返回值都为NULL,NULL作为布尔值的时候,不为1也不为0
/*
mysql> select * from test1;
+------+------+
| a | b |
+------+------+
| 1 | 1 |
| 1 | NULL |
| NULL | NULL |
+------+------+
3 rows in set (0.00 sec)
*/
select * from test1 where a in (null);
/*
Empty set (0.00 sec)
*/
select * from test1 where a in (null,1);
/*
+------+------+
| a | b |
+------+------+
| 1 | 1 |
| 1 | NULL |
+------+------+
2 rows in set (0.00 sec)
*/
-- 当IN和NULL比较时,无法查询出为NULL的记录。
select * from test1 where a not in (1);
/*
Empty set (0.00 sec)
*/
select * from test1 where a not in (null);
/*
Empty set (0.00 sec)
*/
select * from test1 where a not in (null,2);
/*
Empty set (0.00 sec)
*/
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的查询结果都为空
/*
mysql> select * from test1;
+------+------+
| a | b |
+------+------+
| 1 | 1 |
| 1 | NULL |
| NULL | NULL |
+------+------+
3 rows in set (0.00 sec)
select * from test2;
+------+------+
| a | b |
+------+------+
| 1 | 1 |
| 1 | NULL |
| NULL | NULL |
+------+------+
3 rows in set (0.00 sec)
*/
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)
*/
-- 查询语句中使用exists、not exists对比test1.a=test2.a,因为=不能比较NULL,结果和预期一致
- 判断NULL只能用IS NULL、IS NOT NULL
select 1 is not null;
/*
+---------------+
| 1 is not null |
+---------------+
| 1 |
+---------------+
1 row in set (0.00 sec)
*/
select 1 is null;
/*
+-----------+
| 1 is null |
+-----------+
| 0 |
+-----------+
1 row in set (0.00 sec)
*/
select null is null;
/*
+--------------+
| null is null |
+--------------+
| 1 |
+--------------+
1 row in set (0.00 sec)
*/
select null is not null;
/*
+------------------+
| null is not null |
+------------------+
| 0 |
+------------------+
1 row in set (0.00 sec)
*/
-- 判断是否为空只能用IS NULL、IS NOT NULL。
- count(字段)无法统计字段为NULL的值,count(*)可以统计值为null的行
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。
*/
select * from test1 where a is null;
/*
+------+------+
| a | b |
+------+------+
| NULL | NULL |
+------+------+
1 row in set (0.00 sec)
*/
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的行
- 当字段为主键的时候,字段会自动设置为not null
总结
- NULL作为布尔值的时候,不为1也不为0
- 任何值和NULL使用运算符(>、<、>=、<=、!=、<>)或者(in、not in、any/some、all),返回值都为NULL
- 当IN和NULL比较时,无法查询出为NULL的记录
- 当NOT IN 后面有NULL值时,不论什么情况下,整个sql的查询结果都为空
- 判断是否为空只能用IS NULL、IS NOT NULL
- count(字段)无法统计字段为NULL的值,count(*)可以统计值为null的行
- 当字段为主键的时候,字段会自动设置为not null
- NULL导致的坑让人防不胜防,强烈建议创建字段的时候字段不允许为NULL,给个默认值
网友评论