zoukankan      html  css  js  c++  java
  • 子查询语法详解

    子查询所要解决的问题:不能一步求解的问题!
    语法:select 语句的嵌套
    1.单行子查询:如果子查询只返回一行记录,就是单行子查询
    单行操作符: = , > , >=,<,<=,<>
    2.多行子查询:如果子查询返回多行记录,就是多行子查询
    多行操作符: in(set) , any , all
    如:查询工资比SCOTT高的员工信息,分两步
    1.查询SCOTT的工资: select sal from emp where ename='SCOTT';
    2. 查询比3000高的员工: select * from emp where sal>3000;
    子查询: select * from emp where sal > (select sal from emp where ename='SCOTT');
    //多行子查询
    1.in 在集合中
    select * from emp where deptno in (select deptno from dept where dname='SALES'or dname='ACCOUNTNG');
    select e.* from emp e,dept d where e.deptno=d.deptnoand (d.dname='SALES' or d.dname='ACCOUNTING');
    2.any: 和集合中任意一个值比较
    //查询工资比30号部门任意一个员工高的员工信息
    select * from emp where sal > any (select sal from emp where deptno=30);
    //大于集合任意一个值,只需要大于集合最小值即可
    select * from emp where sal > (select min(sal) from emp where deptno=30)
    3. all:和集合中的所有值比较
    //查询工资比30号部门所有员工高的员工信息
    select * from emp where sal > all (select sal from emp where deptno=30);
    或 select * from emp where sal > (select max(sal) from emp where deptno=30)
    注意的问题:
    1. 要有括号
    2.合理的书写风格
    3.可以在主查询的where ,select, having ,from后面都可以使用子查询
    select 语句后面使用子查询,只能使用单行子查询,即只允许返回一条记录
    4.不可以在group by后面使用子查询
    5.强调from后面的子查询
    6.主查询和子查询可以不是同一张表;只要子查询返回的结果主查询可以使用 即可
    7.一般不在子查询中排序;但在top-n分析问题中,必须对子查询排序
    8.一般先执行子查询 再执行主查询;但相关子查询例外
    9.单行子查询只能使用单行操作符;多行子查询只能使用多行操作
    主查询可以有多个子查询,即1:n关系,子查询可以嵌套用,最多855层
    10.子查询中的null ,为什么集合中若有空值,不能用not in(10,20,null) 可以用in( );
    举例:和上面序号对应
    3. //select 语句后面使用子查询,只能使用单行子查询,即只允许返回一条记录
    select empno,ename,sal,(select job from emp where empno=7839) 第四列 from emp;
    //按部门分组查询最低工资,且最低工资大于10号部门的最低工资才显示
    select deptno ,min(sal) from emp group by deptno having min(sal) >
    (select min(sal) from emp where deptno = 10);
    //查询员工号,姓名,月薪 ,年薪
    select * from (select empno,ename,sal , sal*12 年薪 from emp );
    6.查询部门名称是sales的员工
    select * from emp where deptno = (select deptno from dept where dname='SALES');//子查询方式
    select e.* from emp e,dept d where e.deptno = d.deptno and d.dname='SALES';//多表查询方式
    7.Top-n分析: 找出员工工资表的前三名
    8.一般先执行子查询 再执行主查询;但相关子查询例外
    9.单行子查询只能使用单行操作符;多行子查询只能使用多行操作
    //查询工资最低的员工的职位和薪水
    select ename,job,sal from emp where sal =(select min(sal) from emp);
    //下面是非法的,因为多行子查询使用单行比较符
    select ename from emp where sal = (select min(sal) from emp groupby deptno);错误
    10. 单行子查询空值问题 不能贸然使用 = 或 != ,因为后面返回null就永远不成立
    select ename, job from emp where job=(select job from emp where ename='mike') //子查询可能返回null ,而判断是否为空,不能使用 = 或者 !=
    多行子查询中的null:
    集合有空值,不能用not in 可以用in
    //查询不是领导的员工
    select * from emp where empno not in (select mgr from emp); //错误写法
    select * from emp where empno not in (select mgr from emp where mgr is not null);//正确
    ---------------------
    作者:水河澹澹
    来源:CSDN
    原文:https://blog.csdn.net/qq_33497137/article/details/53791261
    版权声明:本文为博主原创文章,转载请附上博文链接!

  • 相关阅读:
    清除浮动
    版心和布局流程
    浮动(float)
    盒子模型(CSS重点)
    CSS 三大特性
    CSS 背景(background)
    移动web开发之rem布局
    .移动端常见布局
    移动端基础
    CSS高级技巧
  • 原文地址:https://www.cnblogs.com/CandiceW/p/10037366.html
Copyright © 2011-2022 走看看