Mysql——子查询

子查询

Where子查询  单行单列  多行单列

Form子查询  多行多列

查询比SMITH工资高的人

select * from emp where sal > (select sal from emp where ename = 'SMITH');

查询比平均工资高的人

select * from emp where sal > (select avg(sal) from emp);

查询和经理工资相同的员工信息

select * from emp where sal in (select sal from emp where job = 'MANAGER');

查询部门编号,部门位置,部门人数,部门平均工资

第一步:查询每个部门的编号,名称,位置

Dept d

第二步:统计部门人数,平均工资

emp e

e.deptno deptno, count(e.empno) num,avg(e.sal) sal

Group by e.deptno

select e.deptno deptno, count(e.empno) num,avg(e.sal) sal from emp e Group by e.deptno;

将查到的数据和dept组合

select temp.deptno,temp.num,temp.sal,d.dname,d.loc from (select e.deptno deptno, count(e.empno) num,avg(e.sal) sal from emp e Group by e.deptno) temp,dept d where temp.deptno = d.deptno;

通过右连接处理边界数据

select d.deptno,IFNULL(temp.num,0) 部门人数 ,IFNULL(temp.sal,0) 平均工资 ,d.dname,d.loc from (select e.deptno deptno, count(e.empno) num,avg(e.sal) sal from emp e Group by e.deptno) temp right join dept d on temp.deptno = d.deptno;

  • 5
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值