≡
  • 网络编程
  • 数据库
  • CMS技巧
  • 软件编程
  • PHP笔记
  • JavaScript
  • MySQL
位置:首页 > 数据库 > MySQL

MySQL NULL导致的神坑的深入分析

人气:254 时间:2021-07-13

这篇文章主要为大家详细介绍了MySQL NULL导致的神坑的深入分析,具有一定的参考价值,可以用来参考一下。

感兴趣的小伙伴,下面一起跟随四海网的小玲来看看吧!

比较运算符中使用NULL

代码如下:


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)

MySQL NULL导致的神坑

结论:任何值和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)

MySQL NULL导致的神坑

上面3条数据,认真看一下,特别是注意上面NULL的记录。

 

IN、NOT IN和NULL比较

 

 

IN和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)

MySQL NULL导致的神坑

 结论:当IN和NULL比较时,无法查询出为NULL的记录。

 

NOT IN 和NULL比较

 

 

代码如下:


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)

MySQL NULL导致的神坑

结论:当NOT IN 后面有NULL值时,不论什么情况下,整个sql的查询结果都为空。

 

EXISTS、NOT EXISTS和NULL比较

 

代码如下:


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)

MySQL NULL导致的神坑

上面我们复制了表test1创建了表test2。

查询语句中使用exists、not exists对比test1.a=test2.a,因为=不能比较NULL,结果和预期一致。

 

判断NULL只能用IS NULL、IS NOT NULL

 

代码如下:


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)

MySQL NULL导致的神坑

看上面的效果,返回的结果为1或者0。

结论:判断是否为空只能用IS NULL、IS NOT NULL。

 

聚合函数中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)

MySQL NULL导致的神坑

  • 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)

MySQL NULL导致的神坑

上面第1个sql使用is null查询出了结果,第2个sql中count(a)返回的是0行。

结论:count(字段)无法统计字段为NULL的值,count(*)可以统计值为null的行。

 

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

MySQL 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)

MySQL NULL导致的神坑

从上面的脚本可以看出,当字段为主键的时候,字段会自动设置为not null。

结论:当字段为主键的时候,字段会自动设置为not null。

看了上面这些还是比较晕,NULL的情况确实比较难以处理,容易出错,最有效的方法就是避免使用NULL。所以,强烈建议创建字段的时候字段不允许为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,给个默认值

到此这篇关于Mysql NULL导致的神坑的文章就介绍到这了,更多相关Mysql NULL导致坑内容请搜索四海网以前的文章或继续浏览下面的相关文章希望大家以后多多支持四海网!

本文来自:http://www.q1010.com/177/19487-0.html

注:关于MySQL NULL导致的神坑的深入分析的内容就先介绍到这里,更多相关文章的可以留意四海网的其他信息。

关键词:

您可能感兴趣的文章

上一篇:MySQL之select in 子查询优化的简单示例
下一篇:MySQL在linux中实现定时备份的简单示例
热门文章
  • mysql 修改character_set_server为utf-8的简单示例
  • 解决MySQL丢失文件localhost.localdomain.pid、mysql.sock的示例
  • MySQL 数据类型binary和varbinary的简单示例
  • MySQL:reading initial communication packet问题解决方法
  • MySql 表类型MYISAM、InnoDB区别
  • bash: mysql: command not found 的解决方法
  • MYSQL默认用户名ROOT修改方法
  • MySQL 常用命令菜鸟教程
  • MySQL 使用命令行新建用户并授予权限
  • MySql 数据库物理文件存放位置查看示例
  • 最新文章
    • MySQL查看死锁与解除死锁的简单示例
    • MySQL 慢查询的功能实例
    • MySQL查看死锁与去除死锁的简单示例
    • MySQL找出未提交事务的SQL的简单示例
    • MySQL锁阻塞的的简单示例
    • MySQL中的binary类型使用操作的示例
    • SQL优化教程之in与range查询的简单示例
    • MySQL 的 21 个规范、优化最佳实践!
    • MySQL 字符类型大小写敏感的简单示例
    • 解决mybatis-plus分页传入参数后sql where条件没有limit分页信息的问题

四海网收集整理一些常用的php代码,JS代码,数据库mysql等技术文章。