大数据之Hive:hive中的join函数

1.hive中join与mysql中join的异同

mysql中没有left semi join,full join ,但是hive中有;
其他join,left join right join 两者都有;

2.下面重点讲解full join,left semi join

2.1 full join 与 union比较

full join 使用on条件时,select * 相当于把两个表(左表有m列p行和右表有n列q行)的所有列拼接成了一个有m+n列的结果表。

select * from table1 full join table2 on(table1.student_no=table2.student_no);

2)union 相当于把两个查询结果(左查询结果表有m列p行和右查询结果表有n列q行)的所有行进行了拼接,形成具有p+q行的查询结果。

select student_no tb1_student_no,student_name from table1 union select student_no as tb2_student_no,class_no from table2;

备注:union要求两个表的列数相同(要查取的列数)且列的数据类型相同;
3)union和union all的区别
Union:对两个结果集进行并集操作,不包括重复行,同时进行默认规则的排序。
Union All:对两个结果集进行并集操作,包括重复行,不进行排序。

2.2 left semi join

测试子查询:在Hive 2.1.1版本中,是支持where子句中的子查询 in 和 not in;

SELECT table1.student_no, table1.student_name FROM table1 WHERE table1.student_no in (SELECT table2.student_no FROM table2);
SELECT table1.student_no, table1.student_name FROM table1 LEFT SEMI JOIN table2 on ( table1.student_no =table2.student_no);

上面两个语句查询结果相同;
此外,需要注意以下几项:
1、left semi join 的限制是, JOIN 子句中右边的表只能在 ON 子句中设置过滤条件,在 WHERE 子句、SELECT 子句或其他地方过滤都不行。
对右表的过滤条件只能写在on子句中:

hive> SELECT * FROM table1 LEFT SEMI JOIN table2 on ( table1.student_no =table2.student_no and  table2.student_no>3);

3.(Inner) join,left (outer) join,right (outer) join

3.1:测试内连接Inner join等价于join

在这里插入图片描述

select * from table1  join table2 on table1.student_no=table2.student_no;
select * from table1  join table2 on table1.student_no=table2.student_no where table2.student_no is not null ;

3.2:left (outer) join ,在两张表进行连接查询时,会返回左表所有的行,即使在右表中没有匹配的记录。

在这里插入图片描述

select * from table1 left join table2 on(table1.student_no=table2.student_no);

3.3:测试左表独有

在这里插入图片描述

select * from table1 left outer join table2 on table1.student_no=table2.student_no where table2.student_no is null;

3.4:测试right (outer) join 在两张表进行连接查询时,会返回右表所有的行,即使在左表中没有匹配的记录

在这里插入图片描述

select * from table1 right join table2 on(table1.student_no=table2.student_no);

3.5:测试右表独有

在这里插入图片描述

select * from table1 right join table2 on(table1.student_no=table2.student_no) where table1.student_no is  null;

3.6:full join,在两张表进行连接查询时,返回左表和右表中所有没有匹配的行。

在这里插入图片描述

select * from table1 full  join table2 on(table1.student_no=table2.student_no);

3.7:并集去交集

在这里插入图片描述

select * from table1 full join table2 on table1.student_no=table2.student_no where table2.student_no is null or table1.student_no is null ;
  • 5
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 10
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 10
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值