前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >MySQL数据库,详解NULL让人防不胜防的坑(一)

MySQL数据库,详解NULL让人防不胜防的坑(一)

作者头像
用户1289394
发布2021-11-05 09:30:50
2830
发布2021-11-05 09:30:50
举报
文章被收录于专栏:Java学习网

当数据的值为NULL的时候,可能出现各种意想不到的效果,让⼈防不胜防,我们来看看

NULL导致的各种神坑,如何避免?

⽐较运算符中使⽤NULL

认真看下⾯的效果

mysql> select 1>NULL;

+--------+

| 1>NULL |

+--------+

| NULL |

+--------+

1 row in set (0.00 sec)

mysql> select 1<NULL;

这是Mysql系列第13篇。

环境:mysql5.7.25,cmd命令中进⾏演⽰。+--------+

| 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的记录。

本文参与 腾讯云自媒体同步曝光计划,分享自微信公众号。
原始发表:2021-11-01,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 Java学习网 微信公众号,前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
相关产品与服务
云数据库 SQL Server
腾讯云数据库 SQL Server (TencentDB for SQL Server)是业界最常用的商用数据库之一,对基于 Windows 架构的应用程序具有完美的支持。TencentDB for SQL Server 拥有微软正版授权,可持续为用户提供最新的功能,避免未授权使用软件的风险。具有即开即用、稳定可靠、安全运行、弹性扩缩等特点。
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档