子查询分类
标量子查询:查询结果为单行单列
列子查询:一列多行
行子查询:多列单行
表子查询:多行多列
子查询支持规则
select 标量子查询
from 【join】表子查询
where【having】标量子查询、行子查询、列子查询
exists 表子查询
例子
select * from user where department_id =(select department_id from user where id=3)
select * from user where department_id in (select department_id from department where department like ‘A%’)
any(子查询),至少一个满足就可以 all(子查询)所有都满足 一般any跟all可以用其他子查询代替
select * from user where salary >any(select salary from user where id in (1,2,3))
select * from user where salary >all(select salary from user where id in (1,2,3))
相关子查询
select exists (select user_id from user where department_id=3) 结果为0或是1
常用实例 select department_name from department d where exists(select * from u where u.department_id=d.department_id ) #查询出有员工的部门名称