Welcome 微信登录

首页 / 数据库 / MySQL / Oracle exists and not exist

先看下面的例子:Oracle中两个系统表.emp,dept.example:1:not exists(not in)not exists:   这条语句返回select * from scott.dept d where e.deptno=d.deptno and d.deptno=10条件满足的结果集.也就是说,返回的结果集中不存在d.deptno=10结果集的记录,即emp表中没有dept表中d.deptno=10的记录.SQL> select empno,ename,deptno from scott.emp e where not exists(select * from scott.dept d where e.deptno=d.deptno and d.deptno=10);EMPNO ENAME      DEPTNO
----- ---------- ------
7369 SMITH          20
7499 ALLEN          30
7521 WARD           30
7566 JONES          20
7654 MARTIN         30
7698 BLAKE          30
7788 SCOTT          20
7844 TURNER         30
7876 ADAMS          20
7900 JAMES          30
7902 FORD           2011 rows selectednot in:第一个where条件必须给定字段(deptno), 第二个sql语句中,必须明确给定要查询的字段是哪个(deptno).SQL> select empno,ename,deptno from scott.emp e where deptno not in(select deptno from scott.dept d where e.deptno=d.deptno and d.deptno=10);EMPNO ENAME      DEPTNO
----- ---------- ------
7369 SMITH          20
7499 ALLEN          30
7521 WARD           30
7566 JONES          20
7654 MARTIN         30
7698 BLAKE          30
7788 SCOTT          20
7844 TURNER         30
7876 ADAMS          20
7900 JAMES          30
7902 FORD           2011 rows selected2: exists(in)exists:   这条语句返回select * from scott.dept d where e.deptno=d.deptno and d.deptno=10条件满足的记录结果集.也就是说返回的结果集中只存在有d.deptno=10的记录,即emp表中只存在dept表中d.deptno=10的记录.SQL> select empno,ename,deptno from scott.emp e where exists(select * from scott.dept d where e.deptno=d.deptno and d.deptno=10);EMPNO ENAME      DEPTNO
----- ---------- ------
7782 CLARK          10
7839 KING           10
7934 MILLER         10in:第一个where条件必须给定字段(deptno), 第二个sql语句中,必须明确给定要查询的字段是哪个(deptno).SQL> select empno,ename,deptno from scott.emp e where deptno in(select deptno from scott.dept d where e.deptno=d.deptno and d.deptno=10);EMPNO ENAME      DEPTNO
----- ---------- ------
7782 CLARK          10
7839 KING           10
7934 MILLER         10
oracle 中exists (in)和not exists(not in)是判断是否存在和不存在表中记录的关键子.请注意:not in 逻辑上不完全等同于not exists,如果你误用了not in,小心你的程序存在致命的BUG:因此,请尽量不要使用not in(它会调用子查询),而尽量使用not exists(它会调用关联子查询)。Oracle sql plusOracle数据库创造TPC-C性能测试世界纪录相关资讯      oracle 
  • [INS-32052] Oracle基目录和Oracle  (07/22/2014 07:41:41)
  • Oracle 4个大对象(lobs)数据类型  (02/03/2013 12:33:05)
  • Oracle按时间段分组统计  (07/26/2012 10:36:48)
  • [Oracle] dbms_metadata.get_ddl的  (07/12/2013 07:37:30)
  • Liferay Portal 配置使用Oracle和  (07/31/2012 20:07:18)
  • Concurrent Request:Inactive   (07/20/2012 07:44:05)
本文评论 查看全部评论 (2)
表情: 姓名: 字数


评论声明
  • 尊重网上道德,遵守中华人民共和国的各项有关法律法规
  • 承担一切因您的行为而直接或间接导致的民事或刑事法律责任
  • 本站管理人员有权保留或删除其管辖留言中的任意内容
  • 本站有权在网站内转载或引用您的评论
  • 参与本评论即表明您已经阅读并接受上述条款