子查询关键字-ALL、ANY、SOME、IN、EXISTS

子查询关键字-ALL、ANY、SOME、IN、EXISTS

ALL
select from where c > all(查询语句)
等价于
select from where c > result1 and c > result2 and c > result3

特点:
	1:all与子查询返回的所有值比较为true 则返回true
	2:ALL可以与= > < >= <= <>结合使用
	3:all表示指定列中的值必须要大于子查询集中的每一个值
eg:查询年龄大于'1003'部门所有年龄的员工信息
select * from emp3 where age > all(select age from emp3 where dept_id='1003');
   查询不属于任何一个部门的员工信息
select * from emp3 where dept_id != all(select deptno from dept3);
ANY SOME
select from where c > any(查询语句)
等价于
select from where c > result1 or c > result2 or c > result3

特点:
	1:any与子查询返回的所有值比较为true 则返回true
	2:any可以与= > < >= <= <>结合使用
	3:any表示指定列中的值要大于子查询集中任意的一个值
eg:查询年龄大于'1003'部门任意一个员工年龄的员工信息
select * from emp3 where age > any(select age from emp3 where dept_id='1003');
 
some和any的作用是一样的,some可以理解为是any的别名
IN
select from c in (查询语句)
等价于
select from where c =result1 or c=result2 or c=result3

特点:
	in关键字,用于判断某个记录的值,是否在指定的集合中
	在in关键字前面加上not可以将条件反过来
eg:查询研发部和销售部的员工信息,包括员工工号,员工名字
select c.cid,c.name from cmp3 c where dept_id in (select deptno from dept3 where name='研发部' or name='销售部');
EXISTS
select from where exists(查询语句)
    
特点:
	该子查询如果"有数据结果"(至少返回一行数据),则该EXISTS()的结果为true 外层查询执行
	该子查询如果"没有数据结果"(没有任何数据返回),则该EXISTS()的结果为false 外层查询不执行
	注意:EXISTS关键字,比in关键字的运算效率高,在实际开发中 特别是数据量大的时候推荐使用exists关键字

eg:查询公司是否有大于60岁的员工,有则输出
select * from epm3 a where exists (select * from emp3 b where a.age>60)
查询所属部门的员工信息
select *from dept3 a where exists (select * from emp3 b where a.deptno=b.dept_id)

猜你喜欢

转载自blog.csdn.net/qq_44590469/article/details/123676775
今日推荐