Hive join use summary

Summary of hive join mode

join type

[INNER] JOIN inner join
LEFT JOIN left join
RIGHT JOIN right join
FULL OUTER JOIN all outer join
LEFT SEMI JOIN left half join
CROSS JOIN Descartes

Insert picture description here

data preparation

  • cl_tmp.tmp_tm_join_testA
id name
1 Zhang San
2 Li Si
3 Wang Wu
  • cl_tmp.tmp_tm_join_testB
id age
2 26
3 28
4 29
create table cl_tmp.tmp_tm_join_testA(id int,name string);
insert INTO cl_tmp.tmp_tm_join_testA values(1,'张三'),(2,'李四'),(3,'王五');


create table cl_tmp.tmp_tm_join_testB(id int,age int);
insert INTO cl_tmp.tmp_tm_join_testB values(2,26),(3,28),(4,29);

Data association

  • INNER JOIN
# 取 a b 表交集
select * from cl_tmp.tmp_tm_join_testA a INNER JOIN  cl_tmp.tmp_tm_join_testB b ON a.id = b.id;

Insert picture description here

  • LEFT JOIN
# 补充a表中b表列值
select * from cl_tmp.tmp_tm_join_testA a LEFT JOIN  cl_tmp.tmp_tm_join_testB b ON a.id = b.id;

Insert picture description here

# 获取a表中存在,b表中不存在
select * from cl_tmp.tmp_tm_join_testA a LEFT JOIN  cl_tmp.tmp_tm_join_testB b ON a.id = b.id where b.id is null;

Insert picture description here

  • RIGHT JOIN
# 获取b表数据
select * from cl_tmp.tmp_tm_join_testA a RIGHT JOIN  cl_tmp.tmp_tm_join_testB b ON a.id = b.id;

Insert picture description here

# 获取b表中存在,a表中不存在的数据: b - a
select * from cl_tmp.tmp_tm_join_testA a RIGHT JOIN  cl_tmp.tmp_tm_join_testB b ON a.id = b.id where a.id is null;

Insert picture description here

  • FULL [OUTER] JOIN
# a表∪b表
select * from cl_tmp.tmp_tm_join_testA a FULL OUTER JOIN  cl_tmp.tmp_tm_join_testB b ON a.id = b.id;

Insert picture description here

# (a - b) ∪ (b - a) 
select * from cl_tmp.tmp_tm_join_testA a FULL OUTER JOIN  cl_tmp.tmp_tm_join_testB b ON a.id = b.id 
where a.id is null or b.id is null ;
  • LEFT SEMI JOIN
# 同INNER JOIN  
select * from cl_tmp.tmp_tm_join_testA a LEFT SEMI JOIN cl_tmp.tmp_tm_join_testB b ON a.id = b.id;

Insert picture description here

  • CROSS JOIN
# 笛卡尔集
select * from cl_tmp.tmp_tm_join_testA a CROSS JOIN cl_tmp.tmp_tm_join_testB b ;

Insert picture description here
Reference blog

Various join relationships and usage in Hivehttps://blog.csdn.net/leying521/article/details/93197951

Guess you like

Origin blog.csdn.net/dbc_zt/article/details/110230658
Recommended