一、基本概念
in
用来判断某值是否出现在一个特定的集合中。若出现则返回true,否则返回false。
exists
先对where前的主查询进行查询,然后将每一个主查询的结果逐行代入exists查询进行判断。若满足条件则输出当前这一条主查询的结果,否则不输出。
二、实例(以scott用户下的表为例) --财务部或销售部的员工 select * from emp e where e.deptno in
(select deptno from dept d where d.dname ='ACCOUNTING' or d.dname = 'SALES')
select * from emp e where exists
(select 1 from dept d where (d.dname ='ACCOUNTING' or d.dname = 'SALES') andd.deptno = e.deptno)
-- 不是财务部或销售部的员工
select * from emp e where e.deptno not in
(select deptno from dept d where d.dname ='ACCOUNTING' or d.dname = 'SALES')
select * from emp e where not exists
(select 1 from dept d where (d.dname ='ACCOUNTING' or d.dname = 'SALES') andd.deptno = e.deptno)
三 、exists详解
表A
ID NAME
1 A1
2 A2
3 A3
表B
ID AID NAME
1 1 B1
2 2 B2
3 2 B3
SELECT * FROM A WHERE EXISTS (SELECT * FROM B WHERE A.ID = B.AID) 执行结果 1 A1
2 A2
SELECT * FROM A WHERENOTEXIST (SELECT * FROM B WHERE A.ID = B.AID)
执行结果
3 A3
|