hive临时表with_HIVE的多表查询
Hello,⼤家好,今天为⼤家分享⼀份HIVE的多表查询⽂章,之前我们给⼤家简单聊了聊HIVE的⼀些基础操作,但是在我们的实际企业应
⽤中往往没有那么简单容易的,今天就为⼤家分享⼀些HIVE的join语句,union all语句以及with语句的⽤法~
以下⽂章直接来源于以下各站:
blog.csdn/fly910905/article/details/82800533blog.csdn/smile0198/article/details/40051325blog.csdn/qq_22562651/article/d
先来看看union all语句的使⽤
union all语句将俩个表中相同的字段拼接在⼀起进⾏表的查询,注意的是union all不去重,数据会重复,我们来看看⽹络上的⼀些案例~
--准备表create external table IF NOT EXISTS temp_uniontest_ta(a1 string,a2 string)partitioned by (dt string)row format delimited fields terminated by '\t'stored as -- 进⾏union all查询select * from (select a1,a2 from temp_uniontest_ta where dt = '2014-10-13'union allselect a1,a2 from temp_uniontest_tb where dt = '2014-10-
上⾯是我们的union all连接,接下来我们看看hive的join连接
在这⾥要注意的是Hive只⽀持等值连接(equality joins)、外连接(outer joins)和(left/right joins)。Hive 不⽀持所有⾮等值的连接,因为
⾮等值连接⾮常难转化到 map/reduce 任务。另外,Hive ⽀持多于 2 个表的连接。join关联查询实例如下:
innser join(join)查询
selecta.name as aname,a.numb as anumb,b.name as bname,b.nick as bnickfrom t_a ajoin t_b bon a.name=b.name
left outer join(left join)查询
select    a.name as aname,    a.numb as anumb,    b.name as bname,    b.nick as bnickfrom t_a aleft outer join t_b bon a.name=b.name
full outer join(full join)查询
left semi join查询
hive中不⽀持exist/IN⼦查询,可以⽤left semi join来实现同样的效果:
selecta.name as aname,a.numb as anumbfrom t_a aleft semi join t_b bon a.name=b.name;
注意: left semi join的 select⼦句中,不能有右表的字段
上述是我们常⽤的join查询,接下来我们看看我们最常⽤的with语法:
sql中union多表合并
为啥要说with呢,⾸先我们⽤的很多,贼多,hive可以通过with查询来提⾼查询性能,因为先通过with语法将数据查询到内存,然后后⾯其
他查询可以直接使⽤~案例如下:
with q1 as ( select key from q2 where key = '5'),q2 as ( select key from src where key = '5')select * from (select key from q1) a;--上述代码中q1是⼀张表,q2也是⼀--插⼊操作实例create table s1 like src;with q1 as ( select key, value from src where key = '5')from q1insert overwrite table s1select *;
基础的语法就如上所⽰啦,在实际应⽤中我们⼀般会将with,union all,if,统计函数,group by 等结合起来⼀起操作,⼀条sql56⼗⾏
都是正常的,所以建议⼤家打好基础⽅便后期的使⽤~~~~
最终在奉劝图灵的⼴⼤学⼦,别做伸⼿党,想要成长的快点,就必须⾃⼰去操作去练习~学习是没有捷径的~