一 null 不能进行比较操作
结论:对NULL 的处理,只能采用is null或is not null,而不能采用=、in、<、<>、!=、not in这些操作符号。
举例如下:
+----+---------------------+------+----------+
| id | sale_date | num | order_id |
+----+---------------------+------+----------+
| 1 | 2020-05-12 23:23:12 | 12 | 100 |
| 2 | 2020-05-14 23:23:31 | 15 | 100 |
| 3 | 2020-05-15 23:23:50 | 1 | 100 |
| 4 | 2020-05-16 23:24:14 | 10 | 101 |
| 5 | 2020-05-17 23:24:33 | NULL | 101 |
+----+---------------------+------+----------+
5 rows in set
查询1:!= 无法查出列为null的记录。
mysql> SELECT * FROM order_sale_info WHERE order_sale_info.num !=10;
+----+---------------------+-----+----------+
| id | sale_date | num | order_id |
+----+---------------------+-----+----------+
| 1 | 2020-05-12 23:23:12 | 12 | 100 |
| 2 | 2020-05-14 23:23:31 | 15 | 100 |
| 3 | 2020-05-15 23:23:50 | 1 | 100 |
+----+---------------------+-----+----------+
3 rows in set
查询2:= NULL 无法查出数据。
mysql> SELECT * FROM order_sale_info WHERE order_sale_info.num = NULL;
Empty set
二 使用count函数统计存在为null的列时,无法统计入内。
mysql> SELECT COUNT(num) FROM order_sale_info;
+------------+
| COUNT(num) |
+------------+
| 4 |
+------------+
1 row in set
如上例:有5条数据,但count(num)时为4,忽略了null的行。