hive的join,left join,right join,full outer join,left semi join,cross join

注意:Hive中Join的关联键必须在on中指定,不能在Where中指定,否则就会先做笛卡尔积,再过滤。

一、测试数据

测试表

create table test_1223_1
(
mobile    string
,score1   string
,score2   string
)
row format delimited fields terminated by  ',' ;

数据
在这里插入图片描述
测试表

create table test_1223_2
(
mobile    string
,score3   string
,score4   string
)
row format delimited fields terminated by  ',' ;

数据
在这里插入图片描述

二、实验

内关联join

select * 
from test_1223_1 a
join
test_1223_2 b
on a.mobile=b.mobile;

在这里插入图片描述

左关联left join

以LEFT JOIN关键字前面的表作为主表,和其他表进行关联,返回记录和主表的记录数一致,关联不上的字段置为NULL。
hive中,left join与left outer join等价

select * 
from test_1223_1 a
left join
test_1223_2 b
on a.mobile=b.mobile;

在这里插入图片描述

右关联right join

和左外关联相反,以right join关键词后面的表作为主表,和前面的表做关联,返回记录数和主表一致,关联不上的字段为NULL。

select * 
from test_1223_1 a
right join
test_1223_2 b
on a.mobile=b.mobile;

在这里插入图片描述

全外关联full [outer] join

以两个表的记录为基准,返回两个表的记录去重之和,关联不上的字段为NULL。

是否指定OUTER关键字,貌似对查询结果无影响。

注意:FULL JOIN时候,Hive不会使用MapJoin来优化。

select * 
from test_1223_1 a
full outer join
test_1223_2 b
on a.mobile=b.mobile;

在这里插入图片描述

left semi join

以left semi join关键字前面的表为主表,返回主表的KEY也在副表中的记录。

select * 
from test_1223_1 a
left semi join
test_1223_2 b
on a.mobile=b.mobile;

在这里插入图片描述

笛卡尔积关联(cross join)

返回两个表的笛卡尔积结果,不需要指定关联键。

select * 
from test_1223_1 a
cross join
test_1223_2 b
;

在这里插入图片描述
如果指定on

select * 
from test_1223_1 a
cross join
test_1223_2 b
on a.mobile=b.mobile;

在这里插入图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值