hive:幾個join的差別彙總

employee表
在這裏插入圖片描述
department表
在這裏插入圖片描述

1.笛卡爾積

select * from employee a ,department b;

在這裏插入圖片描述
共4*2=8條結果。

2.inner join

select * from employee a 
inner join department b
on a.DepartmentId=b.id; 

等價於

select * from employee a ,department b
where a.DepartmentId=b.id; 

在這裏插入圖片描述

3.left join

返回滿足連接條件的左邊表的所有記錄,若左邊表的某些記錄在右邊表中沒有匹配記錄,右邊表則顯示null。

(1)如果不指定連接條件,left join的結果將和笛卡爾積的結果相同。
select * from employee a 
left join department b;

在這裏插入圖片描述

(2)employee left join department
select * from employee a 
left join department b
on a.DepartmentId=b.id;

在這裏插入圖片描述

(3)department left join employee

交換一下left join左右兩表的位置:
department left join employee的結果 = employee right join department的結果(顯示位置不同)

select * from department a
left join  employee b 
on a.id=b.departmentId;

在這裏插入圖片描述

4.right join

返回滿足連接條件的右邊表的所有記錄,若右邊表的記錄在左邊表中沒有匹配記錄,左邊表則顯示null。

select * from employee a 
right join department b;

在這裏插入圖片描述

select * from employee a 
right join department b
on a.departmentId=b.id;

等價於

select * from employee a 
left join department b
on a.departmentId=b.id
where b.name is not null;

在這裏插入圖片描述

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章