zoukankan      html  css  js  c++  java
  • oracle数据库(四)

    子查询与高级查询

      我们在检索数据库的时候,需要将多个表关联起来进行查询,最常用的有子查询、连接查询和集合查询,子查询可以从另外一个表获取数据,连接查询可以指定多个表的连接方式,集合查询可以将两个或者多个查询返回的行组合起来。

    一.子查询

    1.1在where字句中使用子查询

    select * from scott.emp where deptno = (

    select deptno from scott.emp where empno =7782;

    );

    1.2在having字句中使用子查询

    哪些部门的平均工资小于全体人员的平均工资

    select deptno , avg(sal) from scott.emp group by deptno

    having avg(sal)<(

        select avg(sal) from scott.emp;

    );

    1.3使用in操作符实现指定匹配查询

    已知部门名称为sales和accounting,那么需要获得这两个部门的所有员工信息

    select * from scott.emp where deptno in (

      select deptno from scott.dept where dname in(sales,accounting)

    );

    1.4使用any操作符实现任意匹配查询

    对scott用户的emp表进行操作,获得工资大于任意一个部门的平均工资的员工信息

    select * from scott.emp where sal >any(

    select avg(sal) from scott.emp group by deptno

    );

    1.5使用all操作符实现全部匹配查询

    对scott用户的emp表进行操作,获得工资大于所有部门的平均工资的员工信息

    select * from scott.emp where sal >all(select avg(sal) from scott.emp group by deptno);

    1.6实现关联子查询

    关联子查询会引用外部的一列或者多列,因为它确实与外部语句有关,具体实现时,外部查询的每一行都传递给子查询,子查询以此读取传递过来的每一行的值,将其应用到子查询上,直到外部查询的所有行都处理完为止。

    查询各个部门中,哪些员工的工资低于其所在部门的平均工资

    select * from scott.emp outer where sal<(select avg(sal) from scott.emp inner where inner.deptno=outer.deptno);

    二、高级查询

    检索数据时,通过各个表之间共同列的关联性,可以查询存放在多个表中的不同实体的信息,如果在多个表中进行查询操作,并且指定多个表的连接关系,则该查询就是高级查询。

    2.1使用=实现多个表的简单连接

    连接查询中,如果仅仅通过select字句和from字句连接多个表,那么查询的结果将是一个通过笛卡尔积所生成的表。

    select empno,ename,sal,scott.emp.deptno,scott.emp.deptno,dname from scott.emp ,scott.dept;

    这个简单连接语句结果集就是一个笛卡尔积。

    2.2内连接

           2.2.1等值连接

                   使用INNER JOIN连接两个不同的表scott.emp和scott.dept,ON用来设置连接条件,使用where自句限制查询范围,检索accoutning部门的员工信息

                    select empno ename,sal,d.deptno,dname 

                     from scott.emp e inner join scott.dept

                     on e.deptno=d.deptno

                     where dname = 'ACCOUNTING';

            2.2.2不等值连接

             通过scott用户的emp表和salgrade表,查询员工的工资等级。使用between运算符,建立不等连接

             select empno,ename,sal,grade from scott.emp e inner join scott.salgrade s on e.sal between s.losal and s.hisal;

            2.2.3自然连接

            通过相同的字段将两个表连接在一起,并返回所有符合条件的结果

           select e.empno,e.ename,e.sal,deptno ,d.dname

            from scott.emp e natural join scott.dept d where d.dname= 'ACCOUNTING';

     2.3外连接

     外连接又分为左外连接、右外连接、全外连接三种

    2.3.1左外连接

    select e.empno ,e.name,e.sal,d.grade from scott.emp e left outer jion scott.dept on e.sal between d.losal and d.hisal;

    左外连接在检索结果中除了显示满足连接条件的行外,还要显示join关键字左侧表中所有满足检索条件的表。

    2.3.2右外连接

    select e.deptno,d.deptno from scott.emp e right outer jion scott.dept on e.deptno=d.deptno;

    右外连接在检索结果中除了显示满足连接条件的行外,还要显示join关键字右侧表中所有满足检索条件的表。

    2.3.3全外连接

    全外连接在检索结果中除了显示满足连接条件的行外,还要显示join关键字两侧表中所有满足检索条件的表。

    2.4使用UNION操作符获取两个结果集的并集

    UNION[ALL] 将多个查询结果集合合并,形成一个性的结果集,如果指定ALL,则包括重复的行,反之不包括。

    对scott用户的emp表进行操作,获得员工编号大于7800或者所在部门编号为10的员工信息,使用ORDER BY语句将结果集按照deptno列升序排序输出

    select * from scott.emp where empno >7800

    union all select * from scott.emp where deptno = 10 order by deptno asc;

    2.5使用INTERSECT进行结果集的and运算

    select * from scott.emp where empno >7800 intersect select * from scott.emp where deptno = 10;

    2.6使用MINUS操作符获得结果集的差集

    select * from scott.emp where empno > 7800 minus select * from scott.emp where deptno =10;

  • 相关阅读:
    hibernate_0100_HelloWorld
    MYSQL子查询的五种形式
    JSF是什么?它与Struts是什么关系?
    nop指令的作用
    htmlparser实现从网页上抓取数据(收集)
    The Struts dispatcher cannot be found. This is usually caused by using Struts tags without the associated filter. Struts tags are only usable when the
    FCKeditor 在JSP上的完全安装
    Java遍历文件夹的2种方法
    充电电池和充电时间说明
    吃知了有什么好处
  • 原文地址:https://www.cnblogs.com/zzuli/p/9470234.html
Copyright © 2011-2022 走看看