• 彻底理解Oracle中的集合操作与复合查询


    --Oracle中的复合查询
    复合查询:包含集合运算(操作)的查询
    常见的集合操作有:
    union:    两个查询的并集(无重复行、按第一个查询的第一列升序排序)
    union all:两个查询的并集(有重复行)
    intersect:两个查询的交集(无重复行、按第一个查询的第一列升序排序)
    minus:    两个查询的差集(无重复行、按第一个查询的第一列升序排序),取第一张表有而第二张表没有的所有记录
    
    由于union、intersect、minus存在排序,故而对sql性能的影响很大,建议少用。
    
    --测试
    create table t1 (id number,name varchar2(20));
    create table t2 (id number,name varchar2(20));
    insert into t1 values(1,'表1');
    insert into t1 values(2,'表1');
    insert into t1 values(3,'表1');
    insert into t1 values(4,'表1');
    insert into t1 values(5,'表1');
    insert into t2 values(3,'表2');
    insert into t2 values(4,'表2');
    insert into t2 values(5,'表2');
    insert into t2 values(6,'表2');
    insert into t2 values(7,'表2');
    
    --查询t1和t2
    SQL> select * from t1;
     
            ID NAME
    ---------- --------------------
             1 表1
             2 表1
             3 表1
             4 表1
             5 表1
    
    SQL> select * from t2;
     
            ID NAME
    ---------- --------------------
             3 表2
             4 表2
             5 表2
             6 表2
             7 表2
    
    --并集 
    --1.union                
    select id  from t1
    union
    select id from t2 ;
    
    SQL> select id  from t1
      2  union
      3  select id from t2 ;
     
            ID
    ----------
             1
             2
             3
             4
             5
             6
             7
     
    7 rows selected
    
    --2.union all
    select id  from t1 
    union all
    select id from t2;
    
    SQL> select id  from t1
      2  union all
      3  select id from t2;
     
            ID
    ----------
             1
             2
             3
             4
             5
             3
             4
             5
             6
             7
     
    10 rows selected
    
    --交集
    select id  from t1 
    intersect
    select id from t2;
    
    SQL> select id  from t1
      2  intersect
      3  select id from t2;
     
            ID
    ----------
             3
             4
             5
    
    --差集
    select id  from t1 
    minus
    select id from t2;
    
    SQL> select id  from t1
      2  minus
      3  select id from t2;
     
            ID
    ----------
             1
             2
  • 相关阅读:
    ES6新特性:使用export和import实现模块化
    常见Linux/Unix开发辅助命令什锦
    Spark高速上手之交互式分析
    Lua中的元表与元方法
    explicit 构造函数
    【排序】基数排序(计数排序、桶排序)
    拓展训练—心得体会
    poj3411--Paid Roads(bfs+状压)
    点击单选button后的文字就可以选定相应单选button
    hdu 2349 最小生成树
  • 原文地址:https://www.cnblogs.com/huangbiquan/p/8001751.html
Copyright © 2020-2023  润新知