【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更高效,因為對於左表中一條制定的記錄在右邊表一旦匹配到就停止右邊表的匹配


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM