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. Foreign Exchange(交换生换位置)

     Foreign Exchange Your non-profit organization (iCORE - international Confederation of Revolver Enth ...

  2. 转:PHP - .htaccess设置显示PHP错误

    使用.htaccess可以在某种程度上更改PHP的错误显示的设置,实际上,相当于更改PHP.ini的参数,很是方便. 将以下相应代码放到对应目录中的.htaccess文件,即可实现相应功能. 关闭错误 ...

  3. 哎,就硬盘还不是最掉价的,1999的自配主机,VIRTUALBOX里虚拟机,聊以自慰吧。

    安装时注意的问题,要是不测试MYSQL,则CONFIGURE参数和DISABLE-MYSQL,在编译时有提示的. 然后就是LIBTOOL包过老的问题,以及未安装LIBTOOL包的问题. 最后,是运行命 ...

  4. Android添加桌面快捷方式的简单实现

    核心代码如下: Button bn = (Button) findViewById(R.id.bn); // 为按钮的单击事件添加监听器 bn.setOnClickListener(new OnCli ...

  5. HDU4536 XCOM Enemy Unknown(dfs)

    题目链接. 分析: 用dfs枚举每一波攻击的三个国家. 很暴力,但没想到0ms. #include <iostream> #include <cstdio> #include ...

  6. HDOJ(HDU) 2103 Family planning(需要注意范围)

    Problem Description As far as we known,there are so many people in this world,expecially in china.Bu ...

  7. Nodejs in Visual Studio Code 03.学习Express

    1.开始 下载源码:https://github.com/sayar/NodeMVA Express组件:npm install express -g(全局安装) 2.ExpressRest 打开目录 ...

  8. 关于SQL的Group By

    SELECT col1, col2, col3, sum(col3) from T1 GROUP BY col1, col2, col3, col4 ; 对于含有Group By的Sql语句,需要注意 ...

  9. Maven assembly插件输出文件乱码问题

    使用Maven的<artifactId>maven-assembly-plugin</artifactId>插件导致输出的XML配置文件源文件的中文注释变成乱码,排查了多个地方 ...

  10. app被Rejected 的各种原因翻译。这个绝对有用。

    1. Terms and conditions(法律与条款) 1.1 As a developer of applications for the App Store you are bound by ...