Hive基本查询操作(一)第3关:join操作

17 篇文章 7 订阅
7 篇文章 0 订阅

相关知识

Hive只支持等值连接,即ON子句中只能使用等号连接

假设存在表a:

idname
1bob
2lily
3herry

b:

cidscore
180
290
560

内连接(JOIN)

内连接指的是把符合两边连接条件的数据查询出来:

select a.name,b.score from a join b on a.id=b.cid;

输出结果:

 
  1. bob 80
  2. lily 90

左外连接(LEFT OUTER JOIN

左表全部查询出来,右表不符合连接条件的显示为空:

select a.name,b.score from a left outer join b on a.id=b.cid;

输出结果:

 
  1. bob 80
  2. lily 90
  3. herry null

右外连接(RIGHT OUTER JOIN

右表全部查询出来,左表不符合连接条件的显示为空:

select a.name,b.score from a right outer join b on a.id=b.cid;

输出结果:

 
  1. bob 80
  2. lily 90
  3. null 60

全外连接(FULL OUTER JOIN

左右表符合连接条件和不符合连接条件的都查出来,不符合的显示空:

select a.name,b.score from a full outer join b on a.id=b.cid;

输出结果:

 
  1. bob 80
  2. lily 90
  3. herry null
  4. null 60

左半开连接(LEFT SEMI JOIN)

查询出满足连接条件的左边表记录,需要注意的是selectwhere语句中都不能使用右表的字段。

Hive不支持右半开连接:

select a.name from a LEFT SEMI JOIN b on a.id=b.cid;

输出结果:

 
  1. bob
  2. lily

编程要求

在右侧编辑器中补充SQL,求出表table2中所有城市名的平均工资。(其中库名:db1,表名:table1,表名:table2

table1结构:

INFOTYPE
eduLevel_nameString
company_nameString
jobNameString
salaryint
city_codeint
responsibilityString
workingExpString

table1本地部分文件内容:

 
  1. 本科,北京联通支付有限公司,大数据开发工程师,10000,530,熟练使用hive等,1-3年
  2. 专科,北京联科数创科技有限公司,大数据分析师,8000,530,熟练使用MySQL等数据库,1-3年
  3. 本科,湖南智湘赢播网络技术有限公司,大数据开发工程师,16000,749,熟练使用spark等,3-5年

table2结构:

INFOTYPE
city_codeint
city_nameString

table2本地部分文件内容:

 
  1. 538,上海
  2. 653,杭州
  3. 749,长沙
  4. 763,广州

----------禁止修改----------
create database if not exists db1;
use db1;

create table if not exists table1(
eduLevel_name string comment '学历',
company_name string comment '公司名',
jobName string comment '职位名称',
salary int comment '薪资',
city_code int comment '城市编码',
responsibility string comment '岗位职责',
workingExp string comment '工作经验'
)
row format delimited fields terminated by ','
lines terminated by '\n'
stored as textfile;
truncate table table1;
load data local inpath '/root/t2.txt' into table table1;

create table if not exists table2(
city_code int comment '城市编码',
city_name string comment '城市名'
)
row format delimited fields terminated by ','
lines terminated by '\n'
stored as textfile;
truncate table table2;
load data local inpath '/root/t22.txt' into table table2;
----------禁止修改----------

----------Begin----------
select avg(table1.salary),table2.city_name from table1 right outer join table2 on table1.city_code=table2.city_code group by table2.city_name;



----------End----------



  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Hive中的Full Join是一种操作,它可以将两个表中的所有数据进行合并,并以每个记录的键值进行匹配。Full Join返回的结果集包括两个表中的所有记录,如果某个记录在一个表中存在但在另一个表中不存在,则用NULL值填充。 要实现Hive中的Full Join,可以使用LEFT JOIN和RIGHT JOIN的组合。首先,通过LEFT JOIN将第一个表与第二个表进行联,然后再通过RIGHT JOIN将第二个表与第一个表进行联。这样就可以获取到两个表中的所有记录。 示例代码如下所示: ``` SELECT * FROM table1 LEFT JOIN table2 ON table1.key = table2.key UNION SELECT * FROM table1 RIGHT JOIN table2 ON table1.key = table2.key WHERE table1.key IS NULL; ``` 在这个示例中,table1和table2是要进行联的两个表,key是用于匹配的键值。首先使用LEFT JOIN将table1和table2联起来,然后使用UNION将结果与通过RIGHT JOIN将table2和table1联的结果合并在一起。最后,使用WHERE子句过滤掉在table1中不存在的记录。 通过这种方式,就可以实现Hive中的Full Join操作。 <span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* [hive练习数据和练习题及答案](https://download.csdn.net/download/godchou/10898501)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 50%"] - *2* *3* [Hive Full Join多个表与Union All多个表](https://blog.csdn.net/BIT_666/article/details/110431193)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 50%"] [ .reference_list ]

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值