【hive】子查询

hive中是不支持子查询的

但是并不意味这不支持in 或者 not in

in 或者not in 后边是定值的话是支持的

但是接定制是可以的

例如

select id from table not in(1,2,3)

但是这种是不支持的

select id from table1 not in (

select id from table2 where col1 = ‘a’

)

我们需要用left join来实现

(1)把table1和table2符合条件的数据进行连接

select t1.id as id1,t2.id as id2 from table1 t1 left join(

  select id from table2 where col1 = ‘a’

) t2 on t1.id = t2.id

这时符合条件的table1条目连接的table2条目为null

(2)然后根据in 或者 not in来筛选数据

where

in: id2 is not null

not in: id2 is null

或者使用left semi-join来实现(不支持right semi-join)

left-semi join 返回左边表满足 on 条件的记录

select id from table1 t1 left semi join table2 t2

on t1.id = t2.id and t2.col1 != ‘a’

left-semin join 要比join更高效,因为对于左表中一条制定的记录在右边表一旦匹配到就停止右边表的匹配

原文地址:https://www.cnblogs.com/zzhangyuhang/p/9792683.html