2017-06-23 130 views
2

假设我有2个表格,如下所示。现在,如果我要实现结果的SQL会给使用,insert into B where id not in(select id from A) 将表B.如何在Hive中使用NOT IN

插入3 George如何实现这在蜂巢?

表A

id name  
1 Rahul  
2 Keshav  
3 George 

表B

id name  
1 Rahul  
2 Keshav  
4 Yogesh 
+0

我认为[this](https://stackoverflow.com/questions/20951703/insert-into-where-not-exists-in-hive)是一个很好的参考。 – arcticwhite

+0

Duplicate:https://stackoverflow.com/questions/20880124/hive-command-to-execute-not-in-clause – philantrovert

+0

Philantrovert,arcticwhite谢谢我理解为由philantrovert所反映,它可以用左外连接完成。 – user8167344

回答

2

NOT IN在WHERE与不相关子查询子句是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 

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

+0

嘟嘟,很好的帮助!谢谢大家。 – user8167344

+0

嗨Dudu,你能详细说明'不是空'问题吗? – Amir

+1

@Amir,SQL标准将'x不在(a,b,c)'中作为x <> a和x <> b和x <> c'。如果例如c是NULL,那么'x <> c'是UNKNOWN,因此整个表达式是UNKNOWN。 UNKNOWN被视为FALSE。这意味着无论“x”值是什么,查询都不会返回任何行。 –