1. 先讨论 in 与 not in中存在NULL的状况, sql语句以下:sql
1 select 1 result1 from dual where 1 not in (2, 3); 2 3 4 select 1 result2 from dual where 1 not in (2, 3, null); 5 6 7 select 1 result3 from dual where 1 in (2, 3, null, 1); 8 9 10 select 1 result4 from dual where 1 in (2, 3, null);
执行结果:oracle
result1 | result2 | result3 | result4 |
1 | 没有任何返回值 | 1 | 没有任何返回值 |
说明:in与not in 会跟括号里面的值进行比较是否相等从而得出判断结果,而在oracle中null是没法进行比较的,只能进行判断IS NULL和IS NOT NULL,这就致使in和not in中与null进行比较时会返回false. a in (b, c, d)至关于(a == b) || (a == c) || (a == d), 而 a not in (b, c, d)则至关于(a != b) && (a != c) && (a != d)测试
2. 再来看看exists与 not exists的例子spa
1 select 1 result5 from dual where not exists (select 1 from dual t where t.dummy=null); 2 3 select 1 result6 from dual where exists (select 1 from dual t where t.dummy=null);
执行结果:code
result5 | result6 |
1 | 没有任何返回值 |
说明: exists与not exists至关于一种逻辑判断,exists 的本质就是返回一个布尔值,exists测试关联子查询是否有数据返回,若是有至少一行返回的话则exists判断为真返回true, not exists判断关联子查询是否没有数据返回, 若是没有数据返回则判断为真,返回true。blog
3. 最后看一个有挺有意思的查询,从csdn论坛上看的。table
1 select 'true' from dual where (1,2) not in ((2,3),(2,null)); 2 3 select 'true' from dual where (2,1) not in ((2,3),(2,null)); 4 5 select 'true' from dual where (2,1) not in ((2,3),(null,3)); 6 7 select 'true' from dual where (2,1) not in ((2,3),(null,1));
说明:二元值not in判断,... where (a, b) not in ((c, d), (e, f))相似于((a, b) != (c, d) ) && ((a, b) != (e, f)),将(a, b)与(c, d)比较当作坐标比较,只要有一个坐标对不上这个就是不相等的,所以上面的式子能够扩展成为 (a != c || b != d) && (a != e || b != f)class
4. 稍微总结一下:扩展
5. 以上是我的的一些观点总结,欢迎你们批评指教。select