MySQL中union和join語句應用差別的辨析教程。本站提示廣大學習愛好者:(MySQL中union和join語句應用差別的辨析教程)文章只能為提供參考,不一定能成為您想要的結果。以下是MySQL中union和join語句應用差別的辨析教程正文
union和join是須要結合多張表經常見的聯系關系詞,詳細概念我就不說了,想曉得上彀查就行,由於我也記禁絕確。
先說差異:union對兩張表的操作是歸並數據條數,等因而縱向的,請求是兩張表字段必需是雷同的(Schema of both sides of union should match.)。也就是說假如A表中有三條數據,B表中有兩條數據,那末A union B就會有五條數據。解釋一下union 和union all的差異,關於union假如存在雷同的數據記載會被歸並,而union all不匯合並雷同的數據記載,該有若干筆記錄就會有若干筆記錄。例如在mysql下履行以下語句:
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外面是不克不及履行的,履行select * from tmp_libingxue_a union all select * from tmp_libingxue_b;會failed,hive中union必需在子查詢中停止。如
select * from (select * from tmp_yuwen_a union all select * from tmp_yuwen_b) t1;
留意,必需是union all,零丁用union它會提醒你缺乏ALL,並且前面的t1必需寫,你可以寫成a或許b,然則必定要寫,不寫會失足。
而join則是偏於橫向的結合,僅僅是傾向於,等下具體解釋。join跟union比起來顯得更寬松,對兩個表的字段不做請求,沒無限制前提的join等於兩個表的笛卡爾乘積,一切join須要無限制前提來束縛,經由限制的join就是橫向的擴大了。關於知足限制前提的join會被提掏出來,不知足的直接過濾失落。用法可以很靈巧,上面有兩個簡略的例子:
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用法相似,差別就是left outer join會把右邊表的字段全體選擇出來,左邊表的字段把相符前提的也選擇出來,不知足的全體置空,也就是說以右邊表為參照。right outer join同理以左邊表為參照。這三個join之間的差異說過許多次,網上也有更具體的說明,不再贅述。
雷同點:在某些特定的情形下,可以用join完成union all的功效,這類情形是有前提的,當湧現這類情形的時刻選擇union all照樣group by便可以看情形或許看二者的消費而決議。sql固然就在那末幾個症結詞,但變更多端、功效壯大,只需能完成想要的功效,怎樣用隨意你。需求情形sql簡略重現以下
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
假定有兩個表Table3和Table4,其包括的列和數據分離以下所示。
Table1數據庫表
Table2數據庫表
Table1表和Table2表具有雷同的列構造,是以可使用UNION運算符銜接兩個表的記載集,獲得的銜接成果以下表所示。
應用UNION銜接Table3表和Table4表的記載
上述銜接進程的完成代碼可表現以下:
SELECT * FROM Table1 UNION SELECT * FROM Table2