设为首页 收藏本站
查看: 660|回复: 0

[经验分享] Oracle exists/in和not exists/not in之前的区别与联系

[复制链接]

尚未签到

发表于 2018-9-6 10:03:49 | 显示全部楼层 |阅读模式
  之前写过一篇关于NULL对in和not in结果的影响:Oracle的where条件in/not in中包含NULL时的处理。今天来看看exists和not exists中NULL值对结果的影响。
  网上经常看到关于in和exixts、not in和not exists性能比对和互换的例子,但它们真的就可以简单互换么?我们通过下面的实验来看一下。
  实验环境:Oracle 11.2.0.4
  1、创建表并插入测试数据
create table t1 (id number);  
create table t2 (id number);
  
insert into t1 values(1);
  
insert into t1 values(2);
  
insert into t1 values(3);
  
insert into t1 values(4);
  
insert into t1 values(null);
  
commit;
  
insert into t2 values(3);
  
insert into t2 values(4);
  
insert into t2 values(5);
  
insert into t2 values(6);
  
commit;
  
zx@ORA11G>select * from t1;
  

  ID
  
----------
  1
  2
  3
  4
  

  

  
5 rows selected.
  

  
zx@ORA11G>select * from t2;
  

  ID
  
----------
  3
  4
  5
  6
  

  
4 rows selected.
  第一种情况:exists/in的查询中不包含NULL,外层查询包含NULL
zx@ORA11G>select * from t1 where exists(select 1 from t2 where t1.id=t2.id);  

  ID
  
----------
  3
  4
  

  
2 rows selected.
  

  
zx@ORA11G>select * from t1 where id in (select id from t2);
  

  ID
  
----------
  3
  4
  

  
2 rows selected.
  从上面的查询结果看出exists和in都查到了id=2和3的两条数据。
  第二种情况:not exists/not in的查询中不包含NULL,外层查询包含NULL
zx@ORA11G>select * from t1 where not exists(select 1 from t2 where t1.id=t2.id);  

  ID
  
----------
  

  1
  2
  

  
3 rows selected.
  

  
zx@ORA11G>select * from t1 where id not in (select id from t2);
  

  ID
  
----------
  1
  2
  

  
2 rows selected.
  从上面的结果中可以看到两个查询都查到了id=1和2这两条记录,但not exists还查到了t1表中id为NULL的行。原因是表t1中id为NULL的行exists(3,4,5,6)为False,但前面加了个not则返回结果就为True了。
  第三种情况:exists/in的子查询中包含NULL,外层查询包含NULL
zx@ORA11G>insert into t2 values(null);  

  
1 row created.
  

  
zx@ORA11G>commit;
  

  
Commit complete.
  

  
zx@ORA11G>select * from t1 where id in (select id from t2);
  

  ID
  
----------
  3
  4
  

  
2 rows selected.
  

  
zx@ORA11G>select * from t1 where exists(select 1 from t2 where t1.id=t2.id);
  

  ID
  
----------
  3
  4
  

  
2 rows selected.
  从上面的结果中可以看出exist和in的结果是一致的。
  第四种情况:not exists和not in的查询中包含NULL
zx@ORA11G>select * from t1 where not exists(select 1 from t2 where t1.id=t2.id);  

  ID
  
----------
  

  1
  2
  

  
3 rows selected.
  

  
zx@ORA11G>select * from t1 where id not in (select id from t2);
  

  
no rows selected
  从上面的查询结果中可以看出两个结果差异很大,not exists把id=1和2和为NULL的值都查出来了,而not in查出来的结果为空。no in结果为空的原因可以参考之前的文章,not exists的原因与第二种情况类似。
  第五种情况:not in/not exists的子查询中无NULL值,外层查询也无NULL值
zx@ORA11G>delete from t1 where id is null;  

  
1 row deleted.
  

  
zx@ORA11G>delete from t2 where id is null;
  

  
1 row deleted.
  

  
zx@ORA11G>commit;
  

  
Commit complete.
  

  
zx@ORA11G>select * from t1 where id not in (select id from t2);
  

  ID
  
----------
  1
  2
  

  
2 rows selected.
  

  
zx@ORA11G>select * from t1 where not exists(select 1 from t2 where t1.id=t2.id);
  

  ID
  
----------
  1
  2
  

  
2 rows selected.
  第六种情况:in/exists的子查询中无NULL值,外层查询也无NULL值
zx@ORA11G>select * from t1 where id in (select id from t2);  

  ID
  
----------
  3
  4
  

  
2 rows selected.
  

  
zx@ORA11G>select * from t1 where exists(select 1 from t2 where t1.id=t2.id);
  

  ID
  
----------
  3
  4
  

  
2 rows selected.
  第七种情况:in/exists的子查询中有NULL值,外层查询无NULL值
zx@ORA11G>insert into t2 values(null);  

  
1 row created.
  

  
zx@ORA11G>commit;
  

  
Commit complete.
  

  
zx@ORA11G>select * from t1 where id in (select id from t2);
  

  ID
  
----------
  3
  4
  

  
2 rows selected.
  

  
zx@ORA11G>select * from t1 where exists(select 1 from t2 where t1.id=t2.id);
  

  ID
  
----------
  3
  4
  

  
2 rows selected.
  第八种情况:not in/not exists的子查询中有NULL值,外层查询无NULL值
zx@ORA11G>select * from t1 where id not in (select id from t2);  

  
no rows selected
  

  
zx@ORA11G>select * from t1 where not exists(select 1 from t2 where t1.id=t2.id);
  

  ID
  
----------
  1
  2
  

  
2 rows selected.
  从上面的八种情况我们可以总结如下:
  1、in和exists在有无NULL的情况下可以相互转换。
  2、not in和not exists在都没有NULL值的情况下才可以相互转换。
  参考:https://mp.weixin.qq.com/s/rHKBFMQrrBf1TiUo6UmEmQ
  http://docs.oracle.com/cd/E11882_01/server.112/e41084/conditions013.htm#SQLRF52169
  http://docs.oracle.com/cd/E11882_01/server.112/e41084/conditions012.htm#SQLRF52167



运维网声明 1、欢迎大家加入本站运维交流群:群②:261659950 群⑤:202807635 群⑦870801961 群⑧679858003
2、本站所有主题由该帖子作者发表,该帖子作者与运维网享有帖子相关版权
3、所有作品的著作权均归原作者享有,请您和我们一样尊重他人的著作权等合法权益。如果您对作品感到满意,请购买正版
4、禁止制作、复制、发布和传播具有反动、淫秽、色情、暴力、凶杀等内容的信息,一经发现立即删除。若您因此触犯法律,一切后果自负,我们对此不承担任何责任
5、所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其内容的准确性、可靠性、正当性、安全性、合法性等负责,亦不承担任何法律责任
6、所有作品仅供您个人学习、研究或欣赏,不得用于商业或者其他用途,否则,一切后果均由您自己承担,我们对此不承担任何法律责任
7、如涉及侵犯版权等问题,请您及时通知我们,我们将立即采取措施予以解决
8、联系人Email:admin@iyunv.com 网址:www.yunweiku.com

所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其承担任何法律责任,如涉及侵犯版权等问题,请您及时通知我们,我们将立即处理,联系人Email:kefu@iyunv.com,QQ:1061981298 本贴地址:https://www.iyunv.com/thread-563904-1-1.html 上篇帖子: ubuntu下安装cx_oracle 下篇帖子: linux下oracle手工建库过程
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

扫码加入运维网微信交流群X

扫码加入运维网微信交流群

扫描二维码加入运维网微信交流群,最新一手资源尽在官方微信交流群!快快加入我们吧...

扫描微信二维码查看详情

客服E-mail:kefu@iyunv.com 客服QQ:1061981298


QQ群⑦:运维网交流群⑦ QQ群⑧:运维网交流群⑧ k8s群:运维网kubernetes交流群


提醒:禁止发布任何违反国家法律、法规的言论与图片等内容;本站内容均来自个人观点与网络等信息,非本站认同之观点.


本站大部分资源是网友从网上搜集分享而来,其版权均归原作者及其网站所有,我们尊重他人的合法权益,如有内容侵犯您的合法权益,请及时与我们联系进行核实删除!



合作伙伴: 青云cloud

快速回复 返回顶部 返回列表