0
点赞
收藏
分享

微信扫一扫

MySQL系列——DQL_子查询

迎月兮 2022-04-20 阅读 56
mysql
#一、不关联子查询#
#1单行子查询
select sal from emp
where sal>(select avg(sal) from emp);

#2多行单列子查询(子查询的结果是多条记录,不能使用使用 = > < 进行操作,需要使用 All , Any , in操作符来操作)
select sal from emp
where sal>all(select sal from emp where JOB="CLERK");


#3多行多列子查询
select d.DNAME,d.LOC,temp.tno 部门编号,temp.tnum 部门人数 from dept d join 
( select deptno tno ,count(*) tnum from emp group by deptno) temp on (d.deptno = temp.tno);




#二、关联子查询#
#子查询不能独立运行,先运行外部的查询,再运行子查询
select * from emp e1 where sal > ( select avg(sal) from emp e2 where e2.deptno = e1.deptno);
举报

相关推荐

0 条评论