【轉】Oracle集合操作函數:union、intersect、minus

來源:互聯網
上載者:User

標籤:

集合操作符專門用於合并多條select 語句的結果,包括:UNION, UNION ALL, INTERSECT, MINUS。當使用集合操作符時,必須確保不同查詢的列個數和資料類型匹配。

        集合操作符具有以下注意事項:

  • 集合操作符不適用於LOB、VARRAY和巢狀表格列。
  • UNION、INTERSECT、MINUS操作符不使用於 LONG列。
  • 如果挑選清單中包含有運算式或者函數,那麼必須為運算式或者函數定義列別名。

1、UNION (無重並集):當執行UNION 時,自動去掉結果集中的重複行,並以第一列的結果進行升序排序。

2、UNION ALL (有重並集):不去掉重複行,並且不對結果集進行排序。

3、INTERSECT  (交集):取兩個結果集的交集,並且以第一列的結果進行升序排列。

      select   id,name,job  from worker
      INTERSECT
      select  empno,ename,job  fromemp;

4、MINUS  (差集):只顯示在第一個集合中存在,在第二個集合中不存在的資料。並且以第一列的結果進行升序排序。

5、另外,可以使用order by

     order  by 必須放在最後一條select 語句之後,當列名相同時,可以直接用列名排序,如果不同可以用位置排序,也可以使用別名使其相同。

    select  id, name  x from new_emp
    union all 
    select  empno, ename  x from emp order by x;//列名不同時使用別名排序

   select  id, name  ename from new_emp
   union all 
   select  empno, ename  from emp order by ename;//列名不同時使用別名使其相同後排序

   select  id, name  ename from new_emp
   union all
   select  empno,ename  from emp;//合并後列名顯示以前一個表為主。

【轉】Oracle集合操作函數:union、intersect、minus

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.