1 网站建设的目标是什么,新注册网站,17模板网网页,标题优化seo今天遇到一个问题#xff1a;
1、当 in 内的字段包含 null 的时候#xff0c;正常过滤#xff1b;
2、当 not in 内的字段包含 null 的时候#xff0c;不能正常过滤#xff0c;即使满足条件#xff0c;最终结果也为 空。
测试如下#xff1a;
select * from emp e;当…今天遇到一个问题
1、当 in 内的字段包含 null 的时候正常过滤
2、当 not in 内的字段包含 null 的时候不能正常过滤即使满足条件最终结果也为 空。
测试如下
select * from emp e;当 in 内的字段包含 null 的时候结果正常
select * from emp e where e.mgr in (select comm from emp t);当 not in 内的字段包含 null 的时候结果为空实际应为下面加上 is not null 条件时的结果才算正常
select * from emp e where e.mgr not in (select comm from emp t);加上 is not null 条件时结果正常由于NULL不等于NULL也去除了 MGR 为 NULL 的那条记录所以是12条
select * from emp e where e.mgr not in (select comm from emp t where t.comm is not null);根据以上测试得出用 not in 条件过滤时应首先排除 is not null 的记录否则可能会出现意想不到的结果。
其实当not in中包含null
select * from t where class not in (1,2,null)上面的sql相当于
select * from t where class !1and !2and !null在SQL中NULL值与任何其它值的比较即使是NULL永远不会为“真”。not in 相当于and条件只要有一个false那么所有的都为false所以查出来的数据固定为空
解决方案not in中的数据过滤掉空值 或 使用not exists exists用法
select * from 表A where id in (select id from 表B)-- 上面的sql可以改写为
select * from 表A where exists(select 1 from 表B where 表B.id表A.id)in以子查询表B的结果集为驱动在表A中依次遍历查询id是否在子查询的结果集中存在
exists以外表表A为驱动表若括号内的子查询有任意数据返回表示当前行匹配成功
exists用于检查子查询是否至少会返回一行数据强调的是是否返回结果集不要求知道返回什么