oracle 子查询

多行子查询

  • 单行子查询只返回一行结果
  • 多行子查询返回多行结果
  • 多行子查询结果操作符(in, any, all)
  • any 与任意值比较
  • all 与所有值比较
  • in 属于集合

in

  • in集合中有空值,忽略空值,返回不为空的行;
  • not in 集合中有空值,返回空

any all

  • <all 小于最大
  • >all 大于最小
  • =any 相当于 in
  • >all 大于最大
  • <all 小于最小
  • != all 相当于 not in

多列子查询

select empno, ename, sal, job from emp
    where (job, sal) = (select job, sal from emp where empno=7788)
        and empno <> 7788;
        
select empno, ename, sal, job from emp
    where (job, sal) in (select job, sal from emp where empno=7788)
        and empno <> 7788;

相关子查询

外层值参与内层查询的运算,执行顺序为 外层取值 - 传到内层进行运算 - 内层运算返回值参与外层运算 (循环交错查询)

select e.ename,e.sal,e.deptno
from emp e where e.sal > (select avg(s.sal) from emp s
                                        where e.deptno = s.deptno);

非相关子查询

内层查询与外层查询无关,子查询只执行一次,返回结果后参与外层运算

posted on 2019-03-30 12:19  Digital_life  阅读(121)  评论(0编辑  收藏  举报

导航