1.   先讨论 in 与 not in中存在NULL的情况, 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);

执行结果:

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)

  • result1返回结果1显而易见,1跟2和3相比都不相等, 类似于(1<>2) && (1<>3) 结果为true所以返回结果1
  • result2中(1<>2) && (1<>3) && (1<>NULL)前面两个都是true可最后1跟NULL进行比较为false,一招走错满盘皆输就是这个道理,最终结果为false,因此没有返回结果
  • result3中(1 == 2) || (1 == 3) || (1 == NULL) || (1 == 1)前面三个表达式都是false,但最后一个表达式为true最终结果也就为真了,因此返回1。
  • result4中(1 == 2) || (1 == 3) || (1 == NULL)三个表达式都为false, 最终结果也就为false了, 无任何结果集返回。

2.   再来看看exists与 not exists的例子

 select 1 result5 from dual where not exists (select 1 from dual t where t.dummy=null);

 select 1 result6 from dual where exists (select 1 from dual t where t.dummy=null);

执行结果:

result5 result6
1 没有任何返回值

说明: exists与not exists相当于一种逻辑判断,exists 的本质就是返回一个布尔值,exists测试关联子查询是否有数据返回,如果有至少一行返回的话则exists判断为真返回true, not exists判断关联子查询是否没有数据返回, 如果没有数据返回则判断为真,返回true。

  • result5查询中由于NULL不能与任何值作比较,因此自然是不存在t.dummy=null了,关联查询返回结果集为空,not exists逻辑判断结果为true, 最终1被查询出来。
  • result6查询中存在t.dummy=null, 说不通,关联查询返回结果集为空, 逻辑判断结果为false, 最终外层查询没有任何结果集返回。

3.   最后看一个有挺有意思的查询,从csdn论坛上看的。

 select 'true'  from dual where (1,2) not in ((2,3),(2,null));

 select 'true' from dual where (2,1) not in ((2,3),(2,null));

 select 'true' from dual where (2,1) not in ((2,3),(null,3));

 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)

  • 第1行的查询判断为true && true 结果为true、最终字符'true'得以返回。
  • 第3行的查询判断为true && false 结果为false、最终没有结果返回。
  • 第5行的查询判断为true && true 结果为true、 最终字符'true'得以返回。
  • 第7行的查询判断为true && false 结果为false、 最终没有结果返回。

4.    稍微总结一下:

  • in 在a in (b, c, d, ... , null)中, 括号里面的比较值里面存在NULL的话, 看其它比较值里面是否有跟a相等的值存在, 如果有则返回true, 否则返回false.
  • not in 在 a not in (b, c, d,..., null)中,如果括号里面存在NULL的话, 则一律返回false.
  • exists 在 exists的关联查询条件里面如果存在NULL的话,则内部查询是查询不出结果的,不符合exists至少有一行结果集返回的判断, 因此返回false.
  • not exists 在not exists的关联查询条件里面如果存在NULL的话,则内部查询也是查询不出结果的,符合not exists对于没有结果集返回的预期判断, 因此返回true.

5.    以上是个人的一些观点总结,欢迎大家批评指教。

oralce中exists not exists in not in对于NULL的处理的更多相关文章

  1. Oracle中没有 if exists(...)

    对于Oracle中没有 if exists(...) 的语法,目前有许多种解决方法,这里先分析常用的三种,推荐使用最后一种 第一种是最常用的,判断count(*)的值是否为零,如下declare  v ...

  2. SQL中IN,NOT IN,EXISTS,NOT EXISTS的用法和差别

    SQL中IN,NOT IN,EXISTS,NOT EXISTS的用法和差别: IN:确定给定的值是否与子查询或列表中的值相匹配. IN 关键字使您得以选择与列表中的任意一个值匹配的行. 当要获得居住在 ...

  3. ORACLE 中IN和EXISTS比较

    ORACLE 中IN和EXISTS比较 EXISTS的执行流程      select * from t1 where exists ( select null from t2 where y = x ...

  4. 面试被问之-----sql优化中in与exists的区别

    曾经一次去面试,被问及in与exists的区别,记得当时是这么回答的:''in后面接子查询或者(xx,xx,xx,,,),exists后面需要一个true或者false的结果",当然这么说也 ...

  5. (转)MySQL中In与Exists的区别

    背景:总结mysql相关的知识点. 如果A表有n条记录,那么exists查询就是将这n条记录逐条取出,然后判断n遍exists条件. select * from user where exists s ...

  6. 数据库中in和exists关键字的区别

    数据库中in和exists关键字的区别 in 是把外表和内表作hash join,而exists是对外表作loop,每次loop再对内表进行查询. 一直以来认为exists比in效率高的说法是不准确的 ...

  7. SQL中IN和EXISTS用法的区别

    结论 1. in()适合B表比A表数据小的情况 2. exists()适合B表比A表数据大的情况 当A表数据与B表数据一样大时,in与exists效率差不多,可任选一个使用. select * fro ...

  8. [转]Oracle中没有 if exists(...)

    本文转自:http://blog.csdn.net/hollboy/article/details/7550171 对于Oracle中没有 if exists(...) 的语法,目前有许多种解决方法, ...

  9. SQL查询中in和exists的区别分析

    select * from A where id in (select id from B); select * from A where exists (select 1 from B where ...

随机推荐

  1. HDU1276(士兵队列训练模拟与链表)

    HDU1276 Time Limit:1000MS     Memory Limit:32768KB     64bit IO Format:%I64d & %I64u   Descripti ...

  2. HDU-2054 A==B?

    #include<stdio.h>#include<string.h>char n[100000], m[100000];int main(){ int i, j, len_n ...

  3. python中read、readline和readlines的区别

    read        读取整个文件 readline    读取下一行 readlines   读取整个文件到一个迭代器以供我们遍历(读取到一个list中,以供使用,比较方便). 123.txt内容 ...

  4. 触摸事件 - UIControlEvents

    首先,UIControlEvents有这个几种: UIControlEventTouchDown           = 1 <<  0,      // on all touch dow ...

  5. C# Attribute

    Attribute 是C#非常重要的一块内容,需要研究一下. Attribute  的简单使用:简而言之,就是可以自定义通用标志位,而不是在每个所需的类型中分别增加标志位. //class专用attr ...

  6. poj 1066 Treasure Hunt

    http://poj.org/problem?id=1066 #include <cstdio> #include <cstring> #include <cmath&g ...

  7. UNDO 100%

    另外查了下v$undostat,发现begin_time已经很久没有改变, BEGIN_TIME           END_TIME             MAXQUERYLEN MAXCONCU ...

  8. 【HDOJ】1423 Greatest Common Increasing Subsequence

    LCIS /* 1423 */ #include <cstdio> #include <cstring> #include <cstdlib> #define MA ...

  9. 调用test case集,并生成测试报告

    结构是 test_all.py 进行配置,执行所有测试用例集,并合并测试报告到同一个文件 #test_all.py 进行配置,执行所有测试用例集 # coding = utf-8 from time ...

  10. 《SDN核心技术剖析和实战指南》2.3 OF-CONFIG配置管理协议小结

    OpenFlow协议定义了交换机和控制器交换数据的方式和规范,但并没有定义如何配置和管理必需的网络参数和网络资源,OF-CONFIG的提出就是为了对OpenFlow提供配置管理支持.如下图所示,OF- ...