sql中UNION和UNION ALL的區別

2021-08-26 21:03:04 字數 4492 閱讀 2898

寫sql時我們經常會遇到需要把從多張表查詢的集果集進行合併。這時就用到了union。使用union或union all 時一定要保證查詢的列的一致性 。不然sql會報錯。欄位不一致的話可以用單引號來佔位。

例:

select t102.ci_id as

res_id,

t102.citype_id

asrestype_id,

t102.asset_numb

asres_code,

''as

res_e_name,

t102.ci_name

asres_name,

''as

''as

original_id,

t102.asset_brand

asbrand_id,

t102.asset_brand_name

asbrand_name,

t102.series

asseries_id,

t102.series_name

asseries_name,

t102.model

asspecmodel_id,

t102.model_name

asspecmodel_name,

t102.network

asseczone_id,

t102.instalsite_name

ascomputerroom_name,

t102.instalsite

ascomputerroom_id,

t102.cabinet_no

ascabinet_id,

t102.cabinet_no_name

ascabinet_name,

''as

res_grade,

''as

ipv4_desc,

''as

bg_id,

t102.run_corp_code

asbc_id,

t102.remarks

asres_desc,

t102.enabled_status,

t102.deleted_flag,

''as

ostype_id,

''as

os_version_no,

t102.created_at,

t102.updated_at,

t102.deleted_at

from

ci_t10203 t102

where t102.deleted_flag ='n

'--磁帶庫union

allselect t102.ci_id as

res_id,

t102.citype_id

asrestype_id,

t102.asset_numb

asres_code,

''as

res_e_name,

t102.ci_name

asres_name,

''as

''as

original_id,

t102.asset_brand

asbrand_id,

t102.asset_brand_name

asbrand_name,

t102.series

asseries_id,

t102.series_name

asseries_name,

t102.model

asspecmodel_id,

t102.model_name

asspecmodel_name,

t102.network

asseczone_id,

t102.instalsite_name

ascomputerroom_name,

t102.instalsite

ascomputerroom_id,

t102.cabinet_no

ascabinet_id,

t102.cabinet_no_name

ascabinet_name,

''as

res_grade,

''as

ipv4_desc,

''as

bg_id,

t102.run_corp_code

asbc_id,

t102.remarks

asres_desc,

t102.enabled_status,

t102.deleted_flag,

''as

ostype_id,

''as

os_version_no,

t102.created_at,

t102.updated_at,

t102.deleted_at

from

ci_t10204 t102

where t102.deleted_flag ='n

'

下面就來說明union和union all的區別

準備一張測試資料表。注意mysql中的varchar在oracle中是varchar2

drop

table

ifexists

student;

create

table

student

( id

intprimary

key,

name

varchar2(50) not

null

, score

number

notnull

);insert

into student values(1,'

aaron

',78

);insert

into student values(2,'

bill

',76

);insert

into student values(3,'

cindy

',89

);insert

into student values(4,'

damon

',90

);insert

into student values(5,'

ella

',73

);insert

into student values(6,'

frado

',61

);insert

into student values(7,'

gill

',99

);insert

into student values(8,'

hellen

',56

);insert

into student values(9,'

ivan

',93

);insert

into student values(10,'

jay',90

);commit;

select

*from student where id <

4union

select

*from student where id >

2and id <

6

--查詢結果為
1    aaron    78

2    bill     76

3    cindy    89

4    damon    90

5    ella     73

select

*from student where id <

4union all

select

*from student where id >

2and id <

6--查詢結果為

1    aaron    78

2    bill    76

3    cindy    89

3    cindy    89

4    damon    90

5    ella    73

由此可以看出union和union all的區別在於對重複資料的處理。

union 在進行表鏈結後會篩選掉重複的記錄,所以在表鏈結後會對所產生的集果集進行排序運算,刪除重複的記錄再返回結果集。實際使用時大部分是不會產生重複的記錄。

union all只是簡單的將兩個結果合併就返回。如果返回的結果集中有重複的資料,那麼返回的結果集中就包含有重複資料。

從效能上講union all 要比union快很多,它沒有排序去重的耗時。如果表資料量很大,並且可以確定合併的結果集中不會包含重複資料的話。就使用union all.

我一般使用union all.真出現重複資料了再檢查一下。

sql中union和union all用法

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

SQL中Union和Union All的用法

union 操作符 union 操作符用於合併兩個或多個 select 語句的結果集,請注意,union 內部的 select 語句必須擁有 相同數量的列,列也必須擁有 相似的資料型別,同時,每條 select 語句中的列的 順序必須相同。union跟 join 有些許類似,因為這兩個指令都可以由多...

SQL中UNION和UNION ALL的詳細用法

在開發中,有些資料的儲存可能涉及到分庫分表,查詢的時候,可能需要查詢所有的分表,這個時候,就需要用到union或者union all,下面介紹一下union的用法以及與union all的區別 union操作符用於合併兩個或多個select語句的結果集,這裡需要注意的是 union內部的select...