MySQL中union和join語(yǔ)句使用區(qū)別的辨析教程
union和join是需要聯(lián)合多張表時(shí)常見(jiàn)的關(guān)聯(lián)詞,具體概念我就不說(shuō)了,想知道上網(wǎng)查就行,因?yàn)槲乙灿洸粶?zhǔn)確。
先說(shuō)差別:union對(duì)兩張表的操作是合并數(shù)據(jù)條數(shù),等于是縱向的,要求是兩張表字段必須是相同的(Schema of both sides of union should match.)。也就是說(shuō)如果A表中有三條數(shù)據(jù),B表中有兩條數(shù)據(jù),那么A union B就會(huì)有五條數(shù)據(jù)。說(shuō)明一下union 和union all的差別,對(duì)于union如果存在相同的數(shù)據(jù)記錄會(huì)被合并,而union all不會(huì)合并相同的數(shù)據(jù)記錄,該有多少條記錄就會(huì)有多少條記錄。例如在mysql下執(zhí)行以下語(yǔ)句:
select * from tmp_libingxue_a; name number libingxue 1001 yuwen 1002 select * from tmp_libingxue_b; name number libingxue 1001 feiyao 1003 select * from tmp_libingxue_a union select * from tmp_libingxue_b; libingxue 1001 yuwen 1002 feiyao 1003 select * from tmp_libingxue_a union all select * from tmp_libingxue_b; libingxue 1001 yuwen 1002 libingxue 1001 feiyao 1003
但是這樣在hive里面是不能執(zhí)行的,執(zhí)行select * from tmp_libingxue_a union all select * from tmp_libingxue_b;會(huì)failed,hive中union必須在子查詢中進(jìn)行。如
select * from (select * from tmp_yuwen_a union all select * from tmp_yuwen_b) t1;
注意,必須是union all,單獨(dú)用union它會(huì)提示你缺少ALL,而且后面的t1必須寫(xiě),你可以寫(xiě)成a或者b,但是一定要寫(xiě),不寫(xiě)會(huì)出錯(cuò)。
而join則是偏于橫向的聯(lián)合,僅僅是偏向于,等下詳細(xì)說(shuō)明。join跟union比起來(lái)顯得更寬松,對(duì)兩個(gè)表的字段不做要求,沒(méi)有限制條件的join等于兩個(gè)表的笛卡爾乘積,所有join需要有限制條件來(lái)約束,經(jīng)過(guò)限制的join就是橫向的擴(kuò)張了。對(duì)于滿足限制條件的join會(huì)被提取出來(lái),不滿足的直接過(guò)濾掉。用法可以很靈活,下面有兩個(gè)簡(jiǎn)單的例子:
select * from (select * from tmp_yuwen_a)t1 join (select * from tmp_yuwen_b) t2; select * from tmp_yuwen_a t1 join (select * from tmp_yuwen_b) t2;
left outer join和right outer join用法類似,區(qū)別就是left outer join會(huì)把左邊表的字段全部選擇出來(lái),右邊表的字段把符合條件的也選擇出來(lái),不滿足的全部置空,也就是說(shuō)以左邊表為參照。right outer join同理以右邊表為參照。這三個(gè)join之間的差別說(shuō)過(guò)很多次,網(wǎng)上也有更詳細(xì)的解釋,不再贅述。
相同點(diǎn):在某些特定的情況下,可以用join實(shí)現(xiàn)union all的功能,這種情況是有條件的,當(dāng)出現(xiàn)這種情況的時(shí)候選擇union all還是group by就可以看情況或者看兩者的消耗而決定。sql雖然就在那么幾個(gè)關(guān)鍵詞,但變化多端、功能強(qiáng)大,只要能實(shí)現(xiàn)想要的功能,怎么用隨便你。需求情況sql簡(jiǎn)單重現(xiàn)如下
drop table tmp_libingxue_resource; create external table if not exists tmp_libingxue_resource( user_id string, shop_id string, auction_id string, search_time string )partitioned by (pt string) row format delimited fields terminated by '\t' lines terminated by '\n' stored as sequencefile; drop table tmp_libingxue_result; create external table if not exists tmp_libingxue_result( user_id string, shop_id string, auction_id string, search_time string )partitioned by (pt string) row format delimited fields terminated by '\t' lines terminated by '\n' stored as sequencefile; insert overwrite table tmp_libingxue_result where(pt=20041104) select * from tmp_libingxue_resource;
sudo -u taobao hadoop dfs -rmr /group/tbads/warehouse/tmp_libingxue_result/pt=20041104 sudo -u taobao hadoop jar /home/taobao/dataqa/framework/DailyReport.jar com.alimama.loganalyzer.tool.SeqFileLoader tmp_libingxue_resource.txt hdfs://v039182.sqa.cm4:54310/group/tbads/warehouse/tmp_libingxue_result/pt=20041104/part-00000
hive> select * from tmp_libingxue_resource;
OK 2001 0 11 101 20041104 2002 0 11 102 20041104
hive> select * from tmp_libingxue_result;
OK 2001 0 12 103 20041104 2002 0 12 104 20041104
select user_id,shop_id,max(auction_id),max(search_time) from (select * from tmp_libingxue_resource union all select * from tmp_libingxue_result )t1 group by user_id,shop_id;
2001 0 12 103 2002 0 12 104
select t1.user_id,t1.shop_id,t2.auction_id,t2.search_time from (select * from tmp_libingxue_resource) t1 join (select * from tmp_libingxue_result) t2 on t1.user_id=t2.user_id and t1.shop_id=t2.shop_id;
2001 0 12 103 2002 0 12 104
通過(guò)前面的介紹,使用UNION對(duì)表的結(jié)果集進(jìn)行并運(yùn)算與使用JOIN對(duì)多表進(jìn)行連接,二者有本質(zhì)的不同。
下面給出一個(gè)使用UNION運(yùn)算符連接二表記錄的運(yùn)算實(shí)例。
典型的二表記錄的UNION運(yùn)算
假定有兩個(gè)表Table3和Table4,其包含的列和數(shù)據(jù)分別如下所示。
Table1數(shù)據(jù)庫(kù)表
Table2數(shù)據(jù)庫(kù)表
Table1表和Table2表具有相同的列結(jié)構(gòu),因此可以使用UNION運(yùn)算符連接兩個(gè)表的記錄集,得到的連接結(jié)果如下表所示。
使用UNION連接Table3表和Table4表的記錄
上述連接過(guò)程的實(shí)現(xiàn)代碼可表示如下:
SELECT * FROM Table1 UNION SELECT * FROM Table2
相關(guān)文章
深入分析mysql為什么不推薦使用uuid或者雪花id作為主鍵
這篇文章主要介紹了深入分析mysql為什么不推薦使用uuid或者雪花id作為主鍵,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2020-09-09MYSQL復(fù)雜查詢練習(xí)題以及答案大全(難度適中)
在我們學(xué)習(xí)mysql數(shù)據(jù)庫(kù)時(shí)需要一些題目進(jìn)行練習(xí),下面這篇文章主要給大家介紹了關(guān)于MYSQL復(fù)雜查詢練習(xí)題以及答案的相關(guān)資料,文中通過(guò)實(shí)例代碼介紹的非常詳細(xì),這些練習(xí)題難度適中,需要的朋友可以參考下2022-08-08MySQL中三種關(guān)聯(lián)查詢方式的簡(jiǎn)單比較
這篇文章主要介紹了MySQL中三種關(guān)聯(lián)查詢方式的簡(jiǎn)單比較,即ON和USING還有傳統(tǒng)的FROM...WHERE...,需要的朋友可以參考下2015-06-06MySQL數(shù)據(jù)庫(kù)常見(jiàn)字段類型長(zhǎng)度匯總大全
這篇文章主要給大家介紹了關(guān)于MySQL數(shù)據(jù)庫(kù)常見(jiàn)字段類型長(zhǎng)度匯總大全的相關(guān)資料,需要的朋友可以參考下2024-05-05linux 下配置安裝mysql以及配置【經(jīng)驗(yàn)】
這篇文章主要介紹了linux 下配置安裝mysql以及配置【經(jīng)驗(yàn)】,需要的朋友可以參考下2016-05-05mysql 關(guān)鍵詞相關(guān)度排序方法詳細(xì)示例分析
以下是對(duì)mysql關(guān)鍵詞相關(guān)度排序方法進(jìn)行了詳細(xì)的分析介紹,需要的朋友可以過(guò)來(lái)參考下2013-08-08