1. 程式人生 > 實用技巧 >【oracle】-集合運算:UNION,UNION ALL...

【oracle】-集合運算:UNION,UNION ALL...

在Oracle中提供了三種類型的集合操作: 並(UNION)、交(INTERSECT)、差(MINUS)

  • UNION:求並,重複記錄只顯示一次
  • UNION ALL:求並集,顯示所有記錄資訊。
  • INTERSECT:求交集
  • MINUS:返回兩個查詢結果的差集


​ 以下我們來介紹下關於oracle的集合運算:

一、資料準備

根據emp表資料建立emp10表

create table emp10 as (select * from emp where deptno=10);

當前表資料:

  • emp10表

  • emp表


二、oracle集合運算

1、UNION

  • 說明

    ​ 取並集,重複記錄只顯示一次


  • 寫法
select * from emp union select * from emp10; 

  • 結果


2、UNION ALL

  • 說明

    取並集,顯示所有資料


  • 寫法
select * from emp union all select * from emp10; 

  • 結果


3、INTERSECT

  • 說明

    求交集,兩個集合中公共的部分


  • 寫法
  select * from emp intersect select * from emp10; 

  • 結果


4、MINUS

  • 說明

    求差集,即返回的是emp中有,emp10中沒有的資料


  • 寫法
select * from emp minus select * from emp10; 

  • 結果