hive中的幾種join到底有什么區(qū)別
hive中,幾種join的區(qū)別
數(shù)據(jù):
tom,1
jey,2
lilly,7
lilly,8
tom,1
lilly,3
may,4
bob,5
以上兩個(gè)為數(shù)據(jù),沒有什么意義,全是為了檢測join的使用
看一下兩張表,其實(shí)可以看出來,在name一行有重復(fù)的,也有不重復(fù)的,在id一行1表完全包含2表
1. left join
left join會把左邊的表所有數(shù)據(jù)列出來,當(dāng)左邊表有而右邊表沒有的時(shí)候,就會用null代替

select * from jn1 left join jn2 on jn1.name=jn2.name;
jn1.name jn1.id jn2.name jn2.id
tom 1 tom 1
jey 2 NULL NULL
lilly 7 lilly 3
lilly 8 lilly 3
而右表有左表沒有的就不會顯示了
2. join
join會把兩個(gè)表共有的部分篩選出來

select * from jn1 join jn2 on jn1.name=jn2.name;
jn1.name jn1.id jn2.name jn2.id
tom 1 tom 1
lilly 7 lilly 3
lilly 8 lilly 3
可以看到,共有的部分篩選了出來
3. full join
會把兩者沒有的有的全部數(shù)據(jù)都選出來,沒有的顯示空值

select * from jn1 full join jn2 on jn1.name = jn2.name;
jn1.name jn1.id jn2.name jn2.id
tom 1 tom 1
NULL NULL bob 5
jey 2 NULL NULL
lilly 7 lilly 3
lilly 8 lilly 3
NULL NULL may 4
4. Join…on 1=1
這種情況會有笛卡爾積的產(chǎn)生,就是表1的每一行都會和表2匹配一下,這樣就會產(chǎn)生指數(shù)級的增長

select * from jn1 join jn2 on 1=1;
jn1.name jn1.id jn2.name jn2.id
tom 1 lilly 3
tom 1 bob 5
tom 1 may 4
tom 1 tom 1
jey 2 lilly 3
jey 2 bob 5
jey 2 may 4
jey 2 tom 1
lilly 7 lilly 3
lilly 7 bob 5
lilly 7 may 4
lilly 7 tom 1
lilly 8 lilly 3
lilly 8 bob 5
lilly 8 may 4
lilly 8 tom 1
5. union
會把查詢結(jié)果拼接起來,但是要求兩個(gè)查詢結(jié)果的行數(shù)必須保持一致
否則會報(bào)錯(cuò)
FAILED: SemanticException Schema of both sides of union should match
select * from jn1 union select * from jn2;
_u1.name _u1.id
jey 2
lilly 7
bob 5
lilly 3
lilly 8
tom 1
may 4
6. union all
union會組合起來,那么union all可以不
select * from jn1 union all select * from jn2;
lilly 3
tom 1
may 4
bob 5
tom 1
jey 2
lilly 7
lilly 8
union和union all的區(qū)別
從上述的兩個(gè)結(jié)果就可以看出來了兩者的區(qū)別,union會自動去重處理,所以結(jié)果把重復(fù)的數(shù)據(jù)去掉了,而union all則不會去重。


注意tips:left join會用之后,right join不用說了吧
總結(jié)
到此這篇關(guān)于hive中的幾種join到底有什么區(qū)別的文章就介紹到這了,更多相關(guān)hive的join區(qū)別內(nèi)容請搜索本站以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持本站!
版權(quán)聲明:本站文章來源標(biāo)注為YINGSOO的內(nèi)容版權(quán)均為本站所有,歡迎引用、轉(zhuǎn)載,請保持原文完整并注明來源及原文鏈接。禁止復(fù)制或仿造本網(wǎng)站,禁止在非maisonbaluchon.cn所屬的服務(wù)器上建立鏡像,否則將依法追究法律責(zé)任。本站部分內(nèi)容來源于網(wǎng)友推薦、互聯(lián)網(wǎng)收集整理而來,僅供學(xué)習(xí)參考,不代表本站立場,如有內(nèi)容涉嫌侵權(quán),請聯(lián)系alex-e#qq.com處理。
關(guān)注官方微信