【问题标题】:HQL left outer join for finding records present in one table and absent in otherHQL 左外连接用于查找一个表中存在而其他表中不存在的记录
【发布时间】:2015-09-29 11:32:58
【问题描述】:

我有两个具有相同列的表,我想找出第一个表中存在但第二个表中不存在的记录。两个表之间的键是三列的组合。我正在编写如下 Hive 查询:

*

Select a.x,b.y from table_1 a left outer join table_2 b on
    a.c1=b.c1 and a.c2=b.c2 and a.c3=b.c3
    where isnull(b.c1) or isnull(b.c2) or isnull(b.c3);

*

这个查询是否正确?如果 table_1 中有 100 条记录,其中 50 条与 table_2 匹配,则结果将有来自 table_1 的剩余 50 行或更多行,因为我正在加入多个属性并在 where 条件下使用“OR”。

【问题讨论】:

    标签: sql hadoop hive bigdata


    【解决方案1】:

    假设列不为空,那么您通常会进行一次比较:

    Select a.x, b.y
    from table_1 a left outer join
         table_2 b
         on a.c1 = b.c1 and a.c2 = b.c2 and a.c3 = b.c3
    where b.c1 is null;
    

    然而,返回b.y 是相当无用的。你知道值是NULL。

    【讨论】:

    • 通过使用 NOT EXISTS 我们也可以在这种情况下实现结果
    猜你喜欢
    • 2014-09-09
    • 2018-07-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-06-26
    • 2012-04-14
    • 2010-09-26
    • 2020-12-07
    相关资源
    最近更新 更多