“NOT IN”与“NULL”的邂逅
4.为什么使用“IN”可以返回“正确”的结果?
sec@ora10g> select * from t1 where x in (select x from t2);
X
----------
1
3
这是显然的,因为T2表中确确实实的存在着记录“1”和记录“3”,因此“1”和“3”被返回。不过,注意,NULL依然是一个不确定的状态,因此在T1表中的“2”与NULL比较之后仍然是个不确定的结果,因此“2”是不会被返回的。这里给我们的一个错觉:T1表中的“2”与T2表中NULL不同(他们其实也可能相同)。
5.诸多解决方案
1)排除“NOT IN”子查询中存在的NULL值
sec@ora10g> select * from t1 where x not in (select x from t2 where x is not null);
X
----------
2
2)使用“NOT EXISTS”改写
sec@ora10g> select * from t1 where not exists (select * from t2 where t1.x=t2.x);
X
----------
2
3)使用“外连接”改写
sec@ora10g> select t1.* from t1, t2 where t1.x = t2.x(+) and t2.x is null;
X
----------
2
道理是相通的,想想为什么使用“NOT EXISTS”和“外连接”方法改写后就可以成功?(给大家一个机会,这里不赘述。)
6.小结
这里描述的的案例可以用“陷阱”来形容,不过对于这个“陷阱”我们是有诸多解决方案进行规避的。
这里主要分析组合列 NOT IN 非相关子查询中 NULL 的问题。对于单列,如果 NOT IN 非相关 子查询中有 NULL,则没有结果,可以使用 NOT IN 相关子查询、NOT ...
(1,null); select * from t1 where c2 not in (select c2 from t2); no rows found select * from t1 where not exists (select 1 from t2 where...
(1,null); select * from t1 where c2 not in (select c2 from t2); no rows found select * from t1 where not exists (select 1 from t2 where...
(1,null); select * from t1 where c2 not in (select c2 from t2); no rows found select * from t1 where not exists (select 1 from t2 where...
连接代替not in_计算机软件及应用_IT/计算机_专业资料。用连接代替not in ...(+)=B.mobile and A.mobile is null 等价于 select B.mobile from A ...
Oracle 中 in 和 notin 的巨大区别在很多软件系统中,系统的性能很大程度上由数据库的性能决定。以前也曾 经做过很多次关于性能方面的各种测试,特别是关于 oracle...
SQL 里的 EXISTS与in、not exists与not in_日语学习_外语学习_教育专区。SQL...select * from t1 where exists ( select null from t2 where y = x ) ...
SQL 中 IN,NOT IN,EXISTS,NOT EXISTS 的用法和差别: IN:确定给定的值是否与子查询或列表中的值相匹配。 IN 关键字使您得以选择与列表中的任意一个值匹配的...
用LEFT JOIN 代替 NOT IN 或 NOT EXISTS: SQL> conn scott/tiger Connected....用左连接,结果 testb 表里 TempColum 的值为 NULL: SQL> SELECT a.*,b.id...
NOT IN ( select deptno from emp where deptno is not null) and deptno is not null; Explained SQL> select * from table(dbms_xplan.display); PLAN_...
热门文档