【问题标题】:How to use NOT IN in Hive如何在 Hive 中使用 NOT IN
【发布时间】:2017-11-26 15:09:55
【问题描述】:

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

如何在 hive 中实现这一点?

表 A

id  name      
1   Rahul     
2   Keshav    
3   George

表 B

id  name      
1   Rahul     
2   Keshav    
4   Yogesh   

【问题讨论】:

  • 我认为this 是一个很好的参考。
  • Philantrovert,arcticwhite 谢谢我理解为 philantrovert 提出的,它可以通过左外连接来完成。
  • @arcticwhite - 这个答案已经过时了。
  • @philantrovert - 这个答案已经过时了。

标签: hadoop hive bigdata


【解决方案1】:

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% 确定相关列不包含空值。
一个空值足以使您的查询不返回任何结果。

【讨论】:

  • 你好 Dudu,你能详细说明is not null的问题吗?
  • @Amir,SQL 标准将x not in (a,b,c) 视为x<>a and x<>b and x<>c。例如,如果 c 为 NULL,则 x<>c 为 UNKNOWN,因此整个表达式为 UNKNOWN。 UNKNOWN 被视为 FALSE。这意味着无论x 的值是什么,查询都不会返回任何行。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多