• oracle中join学习个人笔记


    3 joins
    3.2连接条件
    using
    相同的列连接可用using子句,且select列中不能对条件列限定表名
    using子句可使用多个列,如using(a,b)
    SELECT location_id, department.name, location.regional_group
    FROM department JOIN location
    USING (location_id);

    natural join
    无需使用using子句
    select location_id,department.name,location.regional_group
    from location natural join department
    会自动匹配两个表中所有相同的列,影响输出


    3.3连接类型
    cross join 笛卡尔积
    不能带条件,如on子句

    inner join 内连接
    默认的连接,inner可省略

    outer join 外连接
    FROM table1 { LEFT | RIGHT | FULL } [OUTER] JOIN table2
    left:返回table1中的所有记录,table2对应列显示空
    right:返回table2中的所有记录,table1对应列显示空
    full: 不匹配的记录全部显示
    out:默认可缺省,oracle会自动在left,right,full后增加out
       
        left out join
        select d.dept_id,d.name,l.regional_group
        from department d left outer join location l
        on d.location_id=l.location_id
        早期的写法
        select d.dept_id,d.name,l.regional_group
        from department d,location l
        where d.location_id=l.location_id(+)
        返回department表里所有记录
       
       
        right out join
        select d.dept_id,d.name,l.regional_group
        from department d right outer join location l
        on d.location_id=l.location_id
        早期的写法
        select d.dept_id,d.name,l.regional_group
        from department d,location l
        where d.location_id(+)=l.location_id
        返回location表里所有记录
       
        full out join
        两张表不匹配的记录都显示

    相等连接与不等连接
    不等连接示例
    select p.name part_name,c.inv_class inv_class
    from part p join inventory_class c
    on p.unit_cost between c.low_cost and c.high_cost

    self join
    自联接
    Self outer joins
    select e.lname employee, m.lname manager
    from employee e left outer join employee m
    on e.manager_emp_id = m.emp_id

    partition outer joins
    oracle10g的新特性,与lag,lap查询有关系
    select nvl(ee.emp_id, 7782), m.year, m.month, nvl(ee.expense_claim, 0)
      from (select * from months where year = 2002) m
      left outer join (select * from employee_expense where emp_id = 7782) ee
        on m.month = ee.month
       and m.year = ee.year
     order by m.month

    可用partition outer joins实现
    SELECT ee.emp_id, m.year, m.month, NVL(ee.expense_claim, 0)
      FROM (SELECT * FROM months WHERE year = 2002) m
      LEFT OUTER JOIN employee_expense ee PARTITION BY(ee.emp_id)
        ON m.year = ee.year
       AND m.month = ee.month
     ORDER BY ee.emp_id, m.month;
    原理
    首先对employee_expense按emp_id分组,一个值一组
    然后每组都与months表联接,会有多个外联接操作


    3.4joins and subqueries
    联接与子查询
    SELECT supplier_id, name
      FROM supplier s
     WHERE EXISTS (SELECT *
              FROM part p
             WHERE p.inventory_qty < 10
               AND p.supplier_id = s.supplier_id);
    可用以下表联接查询实现          
    SELECT s.supplier_id, s.name
      FROM supplier s
      JOIN part p
        ON p.supplier_id = s.supplier_id
     WHERE p.inventory_qty < 10;

    3.5对联接视图执行DML
    聚合函数 Aggregate functions, such as AVG, COUNT, MAX, MIN, SUM
    分析函数 CUME_DIST
    表联接视图如果有以下情况不能执行DML
    层次查询语句,如start with 或 connect by
    group by 或 order by
    Model查询?
    集合操作符,如union,intersect,minus
    聚合函数
    分析函数
    select部分有子查询或关联查询
    distinct
    有read only选项
    有rownum伪列


    键保留表(key-preserved table)不是表的特性,是对联接视图中的表而言
    对此类视图的dml是对上述表的dml

    转自http://space.itpub.net/777981/viewspace-674911

  • 相关阅读:
    物联网相关期刊会议
    Java O O第十一天
    mac下安装伪分布hadoop2.6.0和hbase1.0.1.1
    Ubuntu下使用openssl为apache2配置ssl
    ubuntu下安装hadoop
    ubuntu 添加应用到Dash启动器
    Ubuntu12.04中安装Oracle JDK和NetBeans的方法
    Ubuntu12.04中在桌面建立指向网页的链接文件的方法
    Ubuntu12.04中Gvim无法固定到启动器的解决办法
    Ubuntu中apt-get安装或更新软件错误的解决办法
  • 原文地址:https://www.cnblogs.com/sumsen/p/2524651.html
Copyright © 2020-2023  润新知