CDA117556

2022-01-18   阅读量: 379

Mysql

子查询

扫码加入数据分析学习群

9、行子查询:select empno, ename,job,deptno

from emp

where (deptno,job)=(select deptno,

job from emp where ename='smith' )
and ename<>'smith';

10、子查询操作符:

not in() 在/不在其中

any 满足其中任意一个

all 都满足

11、列子查询:select * from

emp left join saligrade

on sal between lasal and hisal

where yuangongid not in (select

lingdao id from where is not null) ;

12、any操作符(任意一个): select * from emp where sal >any(select sal from emp where deptno=30) and deptno<>30;

13、all操作符: select * from emp where sal >

all(select sal from emp where deptno=30);

14、from 子查询:

select * from emp left join (select deptno, max(sal) mx from smp group by deptno) st on emp.deptno=st.deptno where sal=mx;

先执行子查询里面的聚合值产生列别名,在外边where按照顺序可以引用列别名;

15、select 子查询:

select 后边字段 是同时显示出来的,select中不能直接引用其他字段列别名。

select * from emp;

select * from emp group by deptno;

select deptno, count(*) 各部门人数,

count(*)/(select count(*) from emp) 各部门占比 from emp group by deptno;

能用单表就用单表查询;效率高。

需要使用子查询的情况:

①where子句中需要使用聚合函数;

② 分组后的结果再分组;多次使用group by 报错

;第一次分组后的结果为一张表;

③笛卡尔积连接的表结果再跟另一张表join连接时

④在where、group by、 having子句中使用开窗函数时;



添加CDA认证专家【维克多阿涛】,微信号:【cdashijiazhuang】,提供数据分析指导及CDA考试秘籍。已助千人通过CDA数字化人才认证。欢迎交流,共同成长!
31.8368 8 0 关注作者 收藏

评论(0)


暂无数据

推荐课程