ハイブ結合モードの概要
結合タイプ
[INNER] JOIN内部結合
LEFTJOIN左結合
RIGHTJOIN右結合
FULLOUTERJOINすべて外部結合
LEFTSEMIJOIN左半分
結合CROSSJOINDescartes
データの準備
- cl_tmp.tmp_tm_join_testA
id | 名前 |
---|---|
1 | 張三豊 |
2 | Li Si |
3 | 王呉 |
- cl_tmp.tmp_tm_join_testB
id | 年齢 |
---|---|
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);
データの関連付け
- 内部結合
# 取 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;
- 左結合
# 补充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;
# 获取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;
- 右結合
# 获取b表数据
select * from cl_tmp.tmp_tm_join_testA a RIGHT JOIN cl_tmp.tmp_tm_join_testB b ON a.id = b.id;
# 获取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;
- 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;
# (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 ;
- 左半結合
# 同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;
- クロス結合
# 笛卡尔集
select * from cl_tmp.tmp_tm_join_testA a CROSS JOIN cl_tmp.tmp_tm_join_testB b ;
リファレンスブログ
Hiveでのさまざまな参加関係と使用法https://blog.csdn.net/leying521/article/details/93197951