如何使用not-in-hive

fdbelqdn  于 2021-06-02  发布在  Hadoop
关注(0)|答案(1)|浏览(303)

假设我有两个表,如下所示。现在,如果我想用sql得到结果, insert into B where id not in(select id from A) 将插入 3 George 在表b中。
如何在hive中实现这一点?
表a

id  name      
1   Rahul     
2   Keshav    
3   George

表b

id  name      
1   Rahul     
2   Keshav    
4   Yogesh
bogh5gae

bogh5gae1#

自3年多前(2014年4月21日)发布的hive 0.13以来,不支持带有不相关子查询的where子句。

select * from A where id not in (select id from B where id is not null);
+----+--------+
| id |  name  |
+----+--------+
|  3 | George |
+----+--------+

在早期版本中,外部表的列应使用表名/别名限定。

hive> select * from A where id not in (select id from B where id is not null);
FAILED: SemanticException [Error 10249]: Line 1:22 Unsupported SubQuery Expression 'id': Correlating expression cannot contain unqualified column references.
hive> select * from A where A.id not in (select id from B where id is not null);
OK
3   George

附笔
在使用not in时你应该加上 is not null 除非您100%确定相关列不包含空值,否则将返回内部查询。
一个空值足以导致查询不返回任何结果。

相关问题