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

简介: 子查询关键字-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)
目录
相关文章
|
存储 Oracle 关系型数据库
ORACLE:根据父id查询所有子孙数据,或者根据子id查询所有父数据(start with connect by prior)
一、需求: 我们在开发中经常遇到一种数据库表的设计:一个表中包含父子信息数据,也就是常说的树形数据. —> 最常见的例子就是省市区一体表,就是通过id、pid、level来进行控制,从而一张表来存储数据.我们进行拿数据的时候,不用再连表拿取,直接通过(start with connect by prior)直接便利就会得到数据.
700 2
ORACLE:根据父id查询所有子孙数据,或者根据子id查询所有父数据(start with connect by prior)
|
存储 网络协议 网络安全
MQTTClient_create函数
MQTTClient_create函数
249 0
Zp
|
SQL
在写左关联时SQL语句出现 Duplicate column name 'NAME'名字重复错误解决方法
在写左关联时SQL语句出现 Duplicate column name 'NAME'名字重复错误解决方法
Zp
439 0
父 beforeCreate -> 父 created -> 父 beforeMount -> 子 beforeCreate -> 子 created -> 子 beforeMount -> 子 mounted -> 父 mounted
父 beforeCreate -> 父 created -> 父 beforeMount -> 子 beforeCreate -> 子 created -> 子 beforeMount -> 子 mounted -> 父 mounted
|
SQL 关系型数据库 Oracle