【Hive】(七)Hive的join

SQL Joins

SQL中的连接查询有inner join(内连接)、left join(左连接)、right join(右连接)、full join(全连接)left semi join(左半连接)五种方式,它们之间其实并没有太大区别,仅仅是查询出来的结果有所不同。

left join(左联接): 返回包括左表中的所有记录和右表中联结字段相等的记录
right join(右联接): 返回包括右表中的所有记录和左表中联结字段相等的记录
inner join(等值连接) : 只返回两个表中联结字段相等的行
full join(全连接): 返回两个表中的所有字段。空缺的字段为NULL
left semi join(左半连接): 并不拼接两张表,两个表对 on 的条件字段做交集,返回前面表的记录
  union和full join on的区别,“full join on 列合并和 union 行合并”:

1) full join 使用on条件时,select * 相当于把两个表(左表有m列p行和右表有n列q行)的所有列拼接成了一个有m+n列的结果表。
2)而union相当于把两个查询结果(左查询结果表有m列p行和右查询结果表有n列q行)的所有行进行了拼接,形成具有p+q行的查询结果。
  测试数据

测试数据如下:
data_table1.txt
1    name1
2    name2
3    name3
4    name4
5    name5
6    name6

data_table2.txt
1    11
1    12
1    13
2    11
2    14
3    12
3    15
4    12
4    13
5    14
5    16
7    13
7    15

left join

在这里插入图片描述

select [fileds] 
from tableA a 
left join tableB b
on a.key=b.key

SQL:

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

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

在这里插入图片描述
在这里插入图片描述

select [fileds] 
from tableA a 
left join tableB b
on a.key=b.key
where b.key IS NOT NULL

SQL

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

在这里插入图片描述

right join

在这里插入图片描述

select [fileds] 
from tableA a 
right join tableB b
on a.key=b.key

SQL:

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

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

结果
在这里插入图片描述
右表独有
在这里插入图片描述

select [fileds] 
from tableA a 
right join tableB b
on a.key=b.key
where a.key IS NULL

SQL

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

结果
在这里插入图片描述

inner join

1)如果有on 条件的话,则两边关联只取交集。

select * 
from tableA a join tableB b 
on a.student_no=b.student_no;

2)笛卡尔积:如果没有on条件的话,则是左表和右表的列通过笛卡尔积的形式表达出来,下面两个sql就是求笛卡尔积:

 select * from tableA join tableB;
 select * from tableA inner join tableB;

比如tableA有m行,tableB有n行,最终的结果将有 m*n行
在这里插入图片描述

select [fileds] 
from tableA a 
inner join tableB b
on a.key=b.key

SQL

select * 
from table1 inner join table2 
on table1.student_no=table2.student_no;

select * 
from table1 join table2 
on table1.student_no=table2.student_no;

在这里插入图片描述

full join

在这里插入图片描述

select [fileds] 
from tableA a 
full outer join tableB b
on a.key=b.key;

SQL

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

在这里插入图片描述
取并集后去交集
在这里插入图片描述

select [fileds] 
from tableA a 
full outer join tableB b
on a.key=b.key
where a.key IS NULL OR b.key IS NULL

SQL

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

在这里插入图片描述

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

在这里插入图片描述

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

left semi join

left semi join 的限制是, JOIN 子句中右边的表只能在 ON 子句中设置过滤条件,在 WHERE 子句、SELECT 子句或其他地方过滤都不行

SQL1:

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

SQL2:

SELECT * 
FROM table1 LEFT SEMI JOIN table2 
on ( table1.student_no =table2.student_no);

结果:
在这里插入图片描述
PS:只存在 left SEMI JOIN,不存在SEMI JOIN 和 right SEMI JOIN。
在这里插入图片描述
例子来源:https://www.pianshen.com/article/5498264012/

  • 1
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值