Oracle 中union的用法

2022-06-29 20:51:09 字數 1107 閱讀 6303

union 指令的目的是將兩個 sql 語句的結果合併起來,可以檢視你要的查詢結果. 

例如: 

select date from store_information 

union 

select date from internet_sales 

注意:union用法中,兩個select語句的字段型別匹配,而且字段個數要相同,如上面的例子,在實際的軟體開發過程,會遇到更複雜的情況,具體請看下面的例子 

select  '1' as type,fl_id,fl_code,fl_cname,flda.fl_parentid from flda 

where zt_id=2006030002 

union  

select  '2' as type,xm_id,xm_code ,xm_cname ,fl_id from xmda 

where exists (select * from (select  fl_id from flda where zt_id=2006030002 ) a where xmda.fl_id=a.fl_id) 

order by type,fl_parentid ,fl_id 

這個句子的意思是將兩個sql語句union查詢出來,查詢的條件就是看xmda表中的fl_id是否和主表flda裡的fl_id值相匹配,(也就是存在). 

union在進行表鏈結後會篩選掉重複的記錄,所以在表鏈結後會對所產生的結果集進行排序運算,刪除重複的記錄再返回結果。 

在查詢中會遇到 union all,它的用法和union一樣,只不過union含有distinct的功能,它會把兩張表了重複的記錄去掉,而union all不會,所以從效率上,union all 會高一點,但在實際中用到的並不是很多. 

表頭會用第乙個連線塊的字段。。。。。。。。。。 

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

從效率上說,union all 要比union快很多,所以,如果可以確認合併的兩個結果集中不包含重複的資料的話,那麼就使用union all,如下: 

盡量使用union all,因為union需要進行排序,去除重覆記錄,效率低

Oracle 中union的用法

例如 select date from store information union select date from internet sales 注意 union用法中,兩個select語句的字段型別匹配,而且字段個數要相同,如上面的例子,在實際的軟體開發過程,會遇到更複雜的情況,具體請看下面...

Oracle中UNION的用法

union 指令的目的是將兩個 sql 語句的結果合併起來,可以檢視你要的查詢結果.例如 select date from store information union select date from internet sales 注意 union用法中,兩個select語句的字段型別匹配,而且...

Oracle 中union的用法

union與union all的區別 2011 05 12 14 53 union與union all的區別 如果我們需要將兩個select語句的結果作為乙個整體顯示出來,我們就需要用到union或者union all關鍵字。union 或稱為聯合 的作用是將多個結果合併在一起顯示出來。union和...