zoukankan      html  css  js  c++  java
  • 数据库的子查询

     多表关联- 99 语法

    92语法的不足:表的过滤条件和连接条件混合在一起,维护麻烦

    数据库的数据时刻变化,根据WHERE子句的执行规则,sql语言也会相应发生变化,

     笛卡尔积的三种形成方式

    1:cross join

    2:natural join 自然连接

    语言环境:两个表有相同名字的列

    就是说主外键字段只有一个

    3:使用关键字 using (条件),

    4:join on+条件 (指定连接条件)

    例如:

    select *

    from dept d join emp e

    on d.deptno = e.deptno

     使用join on语句连接3张表

    例如:

    select e.ename,d.dname,sg.grade

    from dept d join emp e on d.deptno = e.deptno

    join salgrade sg on e.sal between sg.losal and sg.hisal

    where e.ename = 'SCO%T'

    外连接

    99语法中通过关键字 outer ,也按照主从表的位子可分为 left/ right outer。

    2子查询

     子查询

    sql语句中查询可以嵌套,一个查询可以作为另一个查询的条件、表

    子查询的关键在于把子查询当作一场表来看待,外层的语句可以把内嵌的子查询返回的结果当成一张表,子查询可以作为一张虚表使用,子查询用用括号括i起来,增强其可读性,可分为单行子查询和多行子查询。

    1.1.1 单行子查询

    子查询有单行,可以取单行中的一个字段形成单个值用于条件比较。

    -- 查询雇员其薪资在雇员平均薪资以上

    -- [1] 查询员工的平均薪资

    select avg(e.sal) "AVGSAL"

    from emp e

    --[2] 查询满足条件的雇员

    select *

    from emp e

    where e.sal > (select avg(e.sal) "AVGSAL" from emp e)

    1.1.1 多行子查询

    -- 查在雇员中有哪些人是管理者

    --1】查询管理者

    select distinct e.mgr

    from emp e

    where e.mgr is not null

    --2】查询指定列表的信息 in

    select e.*

    from emp e

    where e.empno in (select distinct e.mgr

                             from emp e

                             where e.mgr is not null)

    多行子查询返回的结果可以作为 表 使用,通常结合insome/anyall、exists。

    from 后的子查询

    子查询可以作为一张表用于from 后。

    - 每个部门平均薪水的等级

    --1】部门的平均薪资

    select e.deptno,avg(e.sal) "AVGSAL"

    from emp e

    group by e.deptno

    --2】求等级

    select vt0.deptno,vt0.avgsal,sg.grade

    from (select e.deptno,avg(e.sal) "AVGSAL"

            from emp e

            group by e.deptno) VT0,salgrade sg

    where vt0.avgsal between sg.losal and sg.hisal

    - 99 join on

    select vt0.deptno,vt0.avgsal,sg.grade

    from (select e.deptno,avg(e.sal) "AVGSAL"

            from emp e

            group by e.deptno) VT0 join salgrade sg on vt0.avgsal between sg.losal and sg.hisal

     TOP-N

    把select得到的数据集提取前n条数

    rownum:表示查询的数据集记录的编号,从1开始

     查询前10名雇员

    select e.*,rownum

    from emp e

    where rownum <= 10

    elect e.*,rownum

    from emp e

    where rownum <= 10

    order by e.sal desc

    总结

    【1】order by 一定在整个结果集出现后才执行。

    【2】rownum在结果集出现后才有编号

    select  >  rownum > order by

    elect vt0.*,rownum

    from (select e.*

         from emp e

         order by e.sal desc) VT0

    where rownum <= 10

     

    分页

    求查询6-10号的雇员

    select vt0.*

    from (select e.*,rownum "RN"

            from emp e 

     where rownum <= 10) VT0

    where vt0.rn >= 6

    page=n,pagesize=size的数据

    =>[(n-1)*size+1,n*size]

    select vt0.*

    from (select t.*, rownum “RN”

    from table t 

    where rownum <= n*size) VT0

    where vt0.rn >= (n-1)*size+1

     转列 

     

    4.1得到类似下面的结果

    姓名   语文  数学  英语

    张三    78    88    98

    王五    89    56    89

    select ts.name,

    sum(decode(ts.subject,'语文',ts.score)) "语文",

    sum(decode(ts.subject,'数学',ts.score)) "数学",

    sum(decode(ts.subject,'英语',ts.score)) "英语"

    from test_score ts

    group by ts.name

  • 相关阅读:
    C# 四种基本排序算法(冒泡排序,插入排序,选择排序,快速排序)外加折半排序
    jQuery ajax serialize() 方法
    关于问问题
    VIM键位图
    Highcharts选项配置详细说明文档
    awk处理文件内容格式
    【转】如何写出高性能SQL语句
    PHP合并、追加与连接数组
    如何选择web开发语言
    PHP 数据类型验证和获取
  • 原文地址:https://www.cnblogs.com/gflb/p/10878076.html
Copyright © 2011-2022 走看看