zoukankan      html  css  js  c++  java
  • oracle分组统计、多表查询、子查询和分页查询

    分组统计:

      分组统计需要使用 GROUP BY来分组

      语法:SELECT * |列名 FROM 表名 {WEHRE 查询条件} {GROUP BY 分组字段} ORDER BY 列名 1 ASC|DESC,列名 2...ASC|DESC
        按部门分组,查询出部门名称和部门的员工数量

        select d.deptno, d.dname, count(e.ename) from emp e, dept d where e.deptno = d.deptno group by d.deptno, d.dname;

        查询出部门人数大于 5 人的部门

        select d.deptno, d.dname, count(e.ename) from emp e, dept d where e.deptno = d.deptno group by d.deptno, d.dname having count(ename) > 5;

        查询出部门平均工资大于 2000的部门

        select deptno, avg(sal) from emp group by deptno having avg(sal) > 2000;

    多表查询:

    ---笛卡尔积
    select *
    from emp e, dept d;
    ---等值连接
    select *
    from emp e, dept d
    where e.deptno=d.deptno;
    ---内连接
    select *
    from emp e inner join dept d
    on e.deptno = d.deptno;
    ---查询出所有部门,以及部门下的员工信息。【外连接】
    select *
    from emp e right join dept d
    on e.deptno=d.deptno;
    ---查询所有员工信息,以及员工所属部门
    select *
    from emp e left join dept d
    on e.deptno=d.deptno;
    ---oracle中专用外连接
    select *
    from emp e, dept d
    where e.deptno(+) = d.deptno;

      使用(+)表示左连接或者右连接,当(+)在左边表的关联条件字段上时是右连接,如果是在右边表的关联条件字段上就是左连接。

      自连接查询

    select * from emp;
    ---查询出员工姓名,员工领导姓名
    ---自连接:自连接其实就是站在不同的角度把一张表看成多张表。
    select e1.ename, e2.ename
    from emp e1, emp e2
    where e1.mgr = e2.empno;
    ------查询出员工姓名,员工部门名称,员工领导姓名,员工领导部门名称
    select e1.ename, d1.dname, e2.ename, d2.dname
    from emp e1, emp e2, dept d1, dept d2
    where e1.mgr = e2.empno
    and e1.deptno=d1.deptno
    and e2.deptno=d2.deptno;

    子查询:

    ---子查询
    ---子查询返回一个值
    ---查询出工资和SCOTT一样的员工信息
    select * from emp where sal in
    (select sal from emp where ename = 'SCOTT')
    ---子查询返回一个集合
    ---查询出工资和10号部门任意员工一样的员工信息
    select * from emp where sal in
    (select sal from emp where deptno = 10);
    ---子查询返回一张表
    ---查询出每个部门最低工资,和最低工资员工姓名,和该员工所在部门名称
    ---1,先查询出每个部门最低工资
    select deptno, min(sal) msal
    from emp 
    group by deptno;
    ---2,三表联查,得到最终结果。
    select t.deptno, t.msal, e.ename, d.dname
    from (select deptno, min(sal) msal
          from emp 
          group by deptno) t, emp e, dept d
    where t.deptno = e.deptno
    and t.msal = e.sal
    and e.deptno = d.deptno;

    Rownum 与分页查询:

      使用 分析函数 或者 minus运算符 实现分页更加简单:见 https://www.cnblogs.com/roadlandscape/p/12342458.html

      ROWNUM:表示行号,实际上是一个列,但是这个列是一个伪列,此列可以在每张表中出现,所以不可以用 表名.rownum 来使用,但该列可以起别名,通过别名使用。

    ----oracle中的分页
    ---rownum行号:当我们做select操作的时候,
    --每查询出一行记录,就会在该行上加上一个行号,
    --行号从1开始,依次递增,不能跳着走。
    
    ----排序操作会影响rownum的顺序
    select rownum, e.* from emp e order by e.sal desc
    ----如果涉及到排序,但是还要使用rownum的话,我们可以再次嵌套查询。
    select rownum, t.* from(
    select rownum, e.* from emp e order by e.sal desc) t;
    
    
    ----emp表工资倒叙排列后,每页五条记录,查询第二页。
    ----rownum行号不能写大于等于一个正数。
    select * from(
        select rownum rn, tt.* from(
              select * from emp order by sal desc
        ) tt where rownum<11
    ) where rn>5
  • 相关阅读:
    12月上旬的一些记录
    11月底的记录
    备考 19号
    在android 中开发java.net.SocketException: socket failed: EACCES (Permission denied) 报错
    使用JAVA NIO实现的UDP client和server
    ANSI X9.8标准 PIN xor PAN获取PIN BlOCK
    直接拿来用!最火的iOS开源项目(一)
    Anroid ListView分组和悬浮Header实现
    Android 联网监控抓包工具的制作(tcpdump的使用)
    Android tcpdump抓包应用实现
  • 原文地址:https://www.cnblogs.com/roadlandscape/p/12342897.html
Copyright © 2011-2022 走看看