假设我有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
答案 0 :(得分:7)
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%确定相关列不包含空值。
一个空值足以导致查询不返回任何结果。