多表查询_子查询

子查询

子查询在 SELECT、UPDATE、DELETE 语句内部可以出现SELECT语句。内部的SELECT语句结果可以作为外部语句中条件子句的一部分,也可以作为外部查询的临时表。子查询的类型有:

查询中包查询 (SELECT 有两个以上)

分类
       1.出现的位置
       select 列的位置可以出现子查询语句
       from 数据源也可以是子查询的结果集
       where 结果作为条件存在
       
       2.条件
             看返回结果集的特点:
              a. 单行单列(*****) 值类型结果集  可以使用 > < >= <= <> !=				where/having 中
              b. 多行单列 集合类型结果集 可以使用 > >= < <= any all in not in	
              c. 单行多列 对象类型结果集 = in														where中
              d. 多行多列(*****) 数据源                                 									用在from后面

单行单列
1.查询出公司工资最高的员工的详细信息
   select * from emp where sal = (select max(sal) from emp);
   
2.查询出工资大于平均工资的所有人
   select * from emp where sal > (select avg(sal) from emp);

多行单列
 1.查询出大于30部门所有员工工资的员工信息
   
   select * from emp e
   where e.sal > all(select sal from emp  where deptno = 30); 
   > all 表示比查询出来的结果集中最大的值还要大
   < all 表示比查询出来的结果集中最小的值还要小
   
   select * 
   from emp
   where sal > (select max(sal) from emp where deptno = 30)
   
  2.查询出大于任意一个销售员工资的详细信息
   select * 
   from emp
   where sal > any (select sal from emp where job = '销售员');
   > any 表示比查询出来的结果集中最小的值还要大
   < any 表示比查询出来的结果集中最大的值还要小
   
  3.查询出工资等于销售员工资的员工信息
   select *
   from emp
   where sal not in (select sal from emp where job = '销售员');

单行多列
--- 单行多列
  1.查询出和马云同一个部门,同一个职位的员工信息
  select * from emp where (job,deptno) in (select job,deptno from emp where ename = '马云')
  
  2.查询出所有员工工资大于 30000的编号,姓名,部门名称
  select e.empno,e.ename,(select d.dname from dept d where e.deptno = d.deptno) 部门编号
  from emp e
  where e.sal > 30000;
  注意: select放在字段列表中查询出来的结果只能是一列
 
多行多列
-- 多行多列: 用来作为新的数据源的情况比较多,常用在from后面作为二次查询
   select e.empno,e.ename,e.deptno
   from (select * from emp where deptno = 30) e;
   这样写可以缩小数据源的范围,提高效率
   
   select e.*
   from emp e
   where e.deptno = 30;
--------

   查询每个部门编号  名称 位置  部门人数 平均薪资 
      select  d.deptno,d.dname,d.loc,count(e.empno),avg(e.sal)  
      from emp e,dept d
      where e.deptno = d.deptno
      group by d.deptno,d.dname,d.loc;      
      -- 等值查询   emp  15 和 dept 4 表  === > 15 * 4 = 60 次计算
      
      -- 使用子查询 优化sql 
      先子查询 emp 分组 统计 再 和 dept 管理 
      select e.deptno,e.empno,e.sal from emp e;
      
      -- 分组 + 统计  (结果为 多行多列  当做数据源使用 在from 后面可以使用) 
       select e.deptno,count(e.empno),avg(e.sal) from emp e
       group by e.deptno;
       
       select d.dname,d.deptno,d.loc,temp.nums,temp.avgsal from dept d,(
             select e.deptno dno,count(e.empno) nums,avg(e.sal) avgsal 
             from emp e
             group by e.deptno    -- 12条数据  分组 4条数 
       ) temp 
       where d.deptno = temp.dno;
       
      查询  表dept 4条数 temp4条数据  == > 16 次计算   28次计算 
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值