做公众号排版的网站,娱乐网站的特点,无锡网页推广,用来查数据的网站怎么建设一、概述
1、NULL参与的所有的比较和算术运算符(,,,,,,,-,*,/) 结果为unknown#xff1b;
2、unknown的逻辑运算(AND、OR、NOT#xff09;遵循三值运算的真值表#xff1b;
3、如果运算结果直接返回用户#xff0c;使用NULL来标识unknown
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