如何在Hive中使用NOT IN

时间:2017-06-23 06:38:00

标签: hadoop hive bigdata

假设我有2个表,如下所示。现在,如果我想获得sql将使用的结果,insert into B where id not in(select id from A) 这将在表B中插入3 George

如何在配置单元中实现此功能?

表A

id  name      
1   Rahul     
2   Keshav    
3   George

表B

id  name      
1   Rahul     
2   Keshav    
4   Yogesh   

1 个答案:

答案 0 :(得分:7)

具有不相关子查询的WHERE子句中的

NOT IN supported since Hive 0.13,它是在3年前的2014年4月21日发布的。

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

P.S。
使用 NOT IN 时,您应该将is not null添加到内部查询中,除非您100%确定相关列不包含空值。
一个空值足以导致查询不返回任何结果。