Oracle之Union與Union all的區別

2021-09-07 22:14:04 字數 4733 閱讀 2892

如果我們需要將兩個select語句的結果作為乙個整體顯示出來,我們就需要用到union或者union all關鍵字。union(或稱為聯合)的作用是將多個結果合併在一起顯示出來。

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

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

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

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

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

下面以例項說明union與union all的區別:

1、首先建立一張jack表:

sql>

create

table

jack 2

( 3 id int

primary

key,

4 name varchar2(30) not

null,

5 score number

notnull6);

表已建立。

ql>

insert

into jack values(1,'

aaron

',78

);sql

>

insert

into jack values(2,'

bill

',76

);sql

>

insert

into jack values(3,'

cindy

',89

);sql

>

insert

into jack values(4,'

damon

',90

);sql

>

insert

into jack values(5,'

ella

',73

);sql

>

insert

into jack values(6,'

frado

',61

);sql

>

insert

into jack values(7,'

gill

',99

);sql

>

insert

into jack values(8,'

hellen

',56

);sql

>

insert

into jack values(9,'

ivan

',93

);sql

>

insert

into jack values(10,'

jay',90

);sql

>

commit

;sql

>

select

*from

jack;

id name score

---------- -------------------- ----------

1 aaron 78

2 bill 76

3 cindy 89

4 damon 90

5 ella 73

6 frado 61

7 gill 99

8 hellen 56

9 ivan 93

10 jay 90

已選擇10行。

2、使用union與union all進行查詢:

sql>

select

*from jack where id<42

union

3select

*from jack where id >

2and id<6;

id name score

---------- -------------------- ----------

1 aaron 78

2 bill 76

3 cindy 89

4 damon 90

5 ella 73

sql>

select

*from jack where id<42

union

all3

select

*from jack where id >

2and id<6;

id name score

---------- -------------------- ----------

1 aaron 78

2 bill 76

3 cindy 89

3 cindy 89

4 damon 90

5 ella 73

已選擇6行。

從上面的兩個查詢中可以看出它們的區別之一在於對重複結果的處理。

3、調整兩個子查的順序:

sql>

select

*from jack where id >

2and id<62

union

3select

*from jack where id<4;

id name score

---------- -------------------- ----------

1 aaron 78

2 bill 76

3 cindy 89

4 damon 90

5 ella 73

sql>

select

*from jack where id >

2and id<62

union

all3

select

*from jack where id<4;

id name score

---------- -------------------- ----------

3 cindy 89

4 damon 90

5 ella 73

1 aaron 78

2 bill 76

3 cindy 89

已選擇6行。

它們兩者的區別之二在於對排序的處理。union all將按照關聯的次序組織資料,而union將進行依據一定規則進行排序。

4、驗證union排序規則(調整一下查詢欄位的順序):

sql>

select score,id,name from jack where id >

2and id<62

union

3select score,id,name from jack where id<4;

score id name

---------- ---------- --------------------735

ella

762bill

781aaron

893cindy

904 damon

可以看到之前的查詢是基於id,name,score的字段順序,那麼結果集將按照id優先進行排序;而現在新的字段順序也改變了產訊結果的排序,由此可以按照union的排序是按照第乙個字段進行排序的,但是我們也可以進行干預,指定按某個字段進行排序。

5、指定某個字段進行排序

sql>

select

*from2(

3select score,id,name from jack where id>

2and id<74

union

5select score,id,name from jack where id<46

union

7select score,id,name from jack where id>88

) 9order

by id desc

; score id name

---------- ---------- --------------------

9010

jay 939

ivan

616frado

735ella

904damon

893cindy

762bill

781aaron

已選擇8行。

Oracle中Union與Union All的區別

如果我們需要將兩個select語句的結果作為乙個整體顯示出來,我們就需要用到union或者union all關鍵字。union 或稱為聯合 的作用是將多個結果合併在一起顯示出來。union和union all的區別是,union會自動壓縮多個結果集合中的重複結果,而union all則將所有的結果全...

Oracle中Union與Union All的區別

如果我們需要將兩個select語句的結果作為乙個整體顯示出來,我們就需要用到union或者union all關鍵字。union 或稱為聯合 的作用是將多個結果合併在一起顯示出來。union和union all的區別是,union會自動壓縮多個結果集合中的重複結果,而union all則將所有的結果全...

Oracle中Union與Union All的區別

如果我們需要將兩個select語句的結果作為乙個整體顯示出來,我們就需要用到union或者union all關鍵字。union 或稱為聯合 的作用是將多個結果合併在一起顯示出來。union和union all的區別是,union會自動壓縮多個結果集合中的重複結果,而union all則將所有的結果全...