当前位置: 首页 > news >正文

= null 和 is null;SQL中关于NULL处理的4个陷阱;三值逻辑

一、概述

1、NULL参与的所有的比较和算术运算符(>,=,<,<>,<=,>=,+,-,*,/) 结果为unknown;

2、unknown的逻辑运算(AND、OR、NOT)遵循三值运算的真值表;

3、如果运算结果直接返回用户,使用NULL来标识unknown

4、如果运算结果是作为条件判断真假,那么需要通过三值逻辑进行运算,并最终通过以下映射逻辑确定整体判定

5、{false、unknown} -> false

6、{true} ->true

7、在UNION 或 INTERSECT等集合运算中,NULL 被视为彼此相等。

二、三值逻辑

在逻辑学中的三值逻辑(three-valued,也称为三元,或三价逻辑,有时缩写为3VL)是几个多值逻辑系统中的其中之一。有三种状态来表示真、假和一个表示不确定的第三值;这相对于基础的二元逻辑(比如布尔逻辑,它只提供真假两种状态)。

三值逻辑有三个真值(true、false、unknown),它的AND、OR、NOT运算的真值表如下:
在这里插入图片描述

三、SQL中关于NULL处理的4个陷阱

1、 比较谓词与NULL

null并不能判断表达式为空, 判断表达式为空应该使用is null
goods表有13条数据,其中13条数据的count字段的值是null

select *from goods --14条
错误写法:
select *from goods g where g.count = null --0条

正确写法:
select *from goods g where g.count is null --13条

错误原因:
原因是:g.count= null的结果是unknown;然后unknown判断真假为false。

g.count = null -> unknown -> false;

2、Case When与NULL

错误写法:
case expr when nulll then ‘值1’
并不能判断字段expr为null时, 给字段exper赋值为’值1’

正确写法:
case when expr is null then ‘值1’

select c_name, case when c_nationcode = ‘us’ then ‘USA’
when c_nationcode = ‘cn’ then ‘China’
when c_nationcode is null then ‘China’
else ‘Others’ end
from customer

3、 NOT IN 与NULL

NOT IN 子查询谓词,如果子查询结果集有空值,NOT IN谓词总为假 ,即sql不返回数据
例如goods表里数据的count字段只有1条数据是有值等于1,其余数据count字段值都是NULL。 worker表有9条数据,只有1条数据和goods表关联,worker.id = goods.count。

错误写法:
–查出0条
select *from worker where id not in (select count from goods)
因为使用NOT IN 时,子查询的结果集里有空值,这个SQL永远返回为空。

正确写法1:在子查询里加上非空限制
–查出8条
select *from worker where id not in (select count from goods where count is not null)

正确写法2:将NOT IN子查询改写为not exists子查询
–查出8条
select * from worker where not exists (select count from goods where count = worker.id)

4、修饰符ALL与NULL

ALL修饰的子查询谓词,如果子查询的结果集中有空值,则该谓词总为false。
假设通过下面的sql来获取订单系统关闭后注册的用户。
错误写法:
select * from customer where c_regdate > all(select o_orderdate from orders)

和上面的NOT IN类似的,由于子查询的结果中存在NULL,这个sql不会返回预期的结果。ALL 运算实际执行时也是与返回的结果集一一比较,然后进行AND的运算,最终结果unknown。而unknown作为条件进行评估是,结果为false.

正确写法1:在子查询里加上非空限制
select * from customer where c_regdate > all(select o_orderdate from orders where o_orderdate is not null)

正确写法2:
将expr > all或expr >= all改写为聚集函数 expr > (select max()…)(如果expr < all或expr <= all,则改写为expr < (select min() …)、
select * from customer where c_regdate > (select max(o_custkey) from orders)

–错误写法:0条
select *from worker where id > all (select count from goods)

–正确写法:8条
select *from worker where id > all (select count from goods where count is not null) --8条
select *from worker where id > all (select max(count) from goods) --8条

select *from worker where id <= all (select max(count) from goods) --1条

select *from worker where id <= all (select min(count) from goods) --1条
select *from worker where id > all (select min(count) from goods) --8条
注意:为了sql 优化不建议用聚集函数。
在这里插入图片描述
在这里插入图片描述

四、总结

1、NULL参与的所有的比较和算术运算符(>,=,<,<>,<=,>=,+,-,*,/) 结果为unknown
2、unknown的逻辑运算(AND、OR、NOT)遵循三值运算的真值表
3、如果运算结果直接返回用户,使用NULL来标识unknown
4、如果运算结果是作为条件判断真假,那么需要通过三值逻辑进行运算,并最终通过以下映射逻辑确定整体判定:
5、 {false、unknown} -> false
6、 {true} ->true

五、场景

接收到外部系统传的车辆配置编码保存在订单表的config_code字段里。在本系统订单表config_code 关联车辆配置表的编码字段code, 在车辆配置表查询内外饰颜色,选装等字段。如果外部系统传的车辆配置编码是空值,那么保存在订单表里的这条数据的config_code字段值也是空。用这条订单数据去关联车辆配置表,就什么也查不出来了。不会报错。

例如:
– goods表和worker表关联,w.id = g.count
–goods表14条数据,13条数据的count字段值为null, 1条数据的count = 1
–worker表9条数据, 数据的id字段值都正常
– 查询结果14条 = goods表和worker表关联的数据量1条 + goods表和worker表没关联的数据量13条
– 查询结果的总数据量=从表关联上主表的数据量+主表没关联上从表的数据量
select g.“name”,g.count ,w.work_number,w.id as wid from
goods g
left join worker w on w.id = g.count

在这里插入图片描述

参考文章:https://zhuanlan.zhihu.com/p/560941002

http://www.lryc.cn/news/404324.html

相关文章:

  • 拖拽上传(预览图片)
  • Oracle 12c新特性 In-Memory Column Store
  • 【数据结构】二叉树———Lesson2
  • mongodb数据导出与导入
  • 电路学习——经典运放电路之滞回比较器(施密特触发器)(2024.07.18)
  • NVIDIA Container Toolkit 安装与配置帮助文档(Ubuntu,Docker)
  • JavaWeb day01-HTML入门
  • 驱动框架——CMSIS第一部分 RTE驱动框架介绍
  • Debezium日常分享系列之:Debezium2.7版本PostgreSQL数据库连接器
  • 保障信息系统安全保护等级调整期间的安全性
  • 实战:shell编程之全量命令练习
  • 在 CentOS 7 上编译安装 Python 3.11
  • Qt 4.8.7 + MSVC 中文乱码问题深入分析
  • IDEA的常见代码模板的使用
  • arcgis怎么选取某个指定区域地方的数据,比如从全国乡镇数据选取长沙市乡镇数据
  • 二、链表(1)
  • KAFKA搭建教程
  • Linux网络——套接字与UdpServer
  • SpringBoot源码深度解析
  • 【Qt】常用控件
  • electron 主进程和渲染进程通信
  • 【ARM】MDK-解决CMSIS_DAP.DLL missing报错
  • CSS 的环境变量函数env()
  • 数学建模--国赛备赛---TOPSIS算法
  • 均值滤波算法及实现
  • 【Apache Doris】周FAQ集锦:第 16 期
  • 单例模式_Golang
  • 代码随想录 day 18 二叉树
  • 降雨量预测 | Matlab基于ARIMA-RBF降雨量预测
  • 包含示例和模板的流程文档指南