Hive命令执行NOT IN子句 [英] Hive command to execute NOT IN clause

查看:358
本文介绍了Hive命令执行NOT IN子句的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有两张表,tab1& TAB2。

  tab1(T1)tab2(T2)
a1 b1
b1 c1
c1 f1
d1 g1

我正在查找表T1中T2中不存在的值。
在这种情况下,输出应该是 a1 d1



我尝试了以下查询

 从tab1中选择不同的tab1.T1左半加入tab2(tab1.T1!= tab2.T2); 


解决方案

  SELECT t1 .str 
FROM tab1 t1
LEFT OUTER JOIN tab2 t2 ON t1.str = t2.str
WHERE t2.str IS NULL;

结果:

  OK 
a1
d1

为什么 t2.str为null 条件存在:左外部联接确保来自第一个表的所有值都包含在结果中。那么当第二个表中没有值时会发生什么:在这种情况下,第二个表中的所有列都被报告为空。



所以在上面的情况我们正在精确地搜索第二个表条目丢失的情况 - 因此我们:


  • 选择其中一个永不为空(又名表2中的列不为空)。

  • 所以:是一个总是存在的列吗?如果没有,请选择另一个

  • 指定条件table1-alias。table1-never-null-column= null。这意味着记录实际上不存在于连接条件中 - 因此我们发现记录仅存在于表1中。


I have two tables,tab1 & tab2.

tab1(T1)  tab2(T2)
a1         b1
b1         c1
c1         f1
d1         g1

I am looking for the values from table T1 that are not present in T2. In this case, the output should be a1 d1

I have tried with the following query but couldn't get the right solution.

select distinct tab1.T1 from tab1 left semi join tab2 on (tab1.T1!=tab2.T2);

解决方案

SELECT t1.str
FROM tab1 t1 
LEFT OUTER JOIN tab2 t2 ON t1.str = t2.str
WHERE t2.str IS NULL;

Result:

OK
a1
d1

"Why is the t2.str is null condition there": Left outer joins ensure that all values from the first table are included in the results. So then what happens when there are no values in the second table: in that case all of the columns from the second table are reported as null.

So in the case above we are searching precisely for the condition that the second table entries are missing - and thus we:

  • Choose one of the never-empty (aka not null) columns from table two.
  • So: is number an always-present column? If not then please choose another one
  • Specify the condition "table1-alias"."table1-never-null-column" = null. That means that the record is actually not present in the join condition - and thus we found the records existing only in table 1.

这篇关于Hive命令执行NOT IN子句的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆