HIVE的6种join用法

给自己留档,基本是从这个博客抄来的。

例子:两个表格

表A:

idname
1张三
2李四
3王五

表B:

idage
120
229
430
join

内关联,只返回两个表中关联上的结果

select a.id, a.name, b.age from a join b on a.id = b.id

得到结果:

a.ida.nameb.age
1张三20
2李四29
left (outer) join

左外关联,关键字outer可省略。以left join关键字前面的表作为主表,和其他表进行关联,返回记录和主表的记录数一致,关联不上的字段置为null。

select a.id, a.name, b.age from a left join b on a.id = b.id

得到结果:

a.ida.nameb.age
1张三20
2李四29
3王五null
right (outer) join

右外关联,关键字outer可省略。与left join相反,以right join关键字后面的表作为主表,和其他表进行关联,返回记录和主表的记录数一致,关联不上的字段置为null。

select a.id, a.name, b.age from a right join b on a.id = b.id

得到结果:

a.ida.nameb.age
1张三20
2李四29
nullnull30
full (outer) join

全外关联,关键字outer可省略。以两个表的记录为基准,返回两个表的记录去重之和,关联不上的字段为null。

select a.id, a.name, b.age from a full join b on a.id = b.id

得到结果:

a.ida.nameb.age
1张三20
2李四29
3王五null
nullnull30
left semi join

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

select a.name, b.name from a left semi join b on a.id = b.id

得到结果:

a.ida.name
1张三
2李四
cross join

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

select a.id, a.name, b.age from a cross join b

得到结果:

a.ida.nameb.age
1张三20
1张三29
1张三30
2李四20
2李四29
2李四30
3王五20
3王五29
3王五30
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值