Oracle中 union 和 union all 的區別

來源:互聯網
上載者:User

標籤:intersect   重複   資料   oracle   rom   操作   代碼   _id   ora   

如果我們需要將兩個select語句的結果作為一個整體顯示出來,我們就需要用到union或者union all關鍵字。

union(或稱為聯合)的作用是將多個結果合并在一起顯示出來。

union和union all的區別是,union會自動壓縮多個結果集合中的重複結果,而union all則將所有的結果全部顯示出來,不管是不是重複。

Union:對兩個結果集進行並集操作,不包括重複行,同時進行預設規則的排序;

Union All:對兩個結果集進行並集操作,包括重複行,不進行排序;

Intersect:對兩個結果集進行交集操作,不包括重複行,同時進行預設規則的排序;

Minus:對兩個結果集進行差操作,不包括重複行,同時進行預設規則的排序。

可以在最後一個結果集中指定Order by子句改變排序方式。

例如:

select employee_id,job_id from employees  

union  

select employee_id,job_id from job_history 

selectemployee_id,job_id from employees

union 

selectemployee_id,job_id from job_history 

以上將兩個表的結果聯合在一起。

這兩個例子會將兩個

select

語句的結果中的重複值進行壓縮,

也就是結果的資料並不是兩條結果的條數的和。如果希望即使重複的結果顯示出來可以使用

union all,

例如:

2.在oracle的scott使用者中有表emp  代碼 1. select * from emp where deptno >= 20 2. union all   3. select * from emp where deptno <=30 select * from emp where deptno>= 20 union all  select * from emp where deptno<= 30 

這裡的結果就有很多重複值了。

 

 

有關union和union all關鍵字需要注意的問題是:union 和 union all都可以將多個結果集合并,而不僅僅是兩個,你可以將多個結果集串起來。

 

Oracle中 union 和 union all 的區別

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.