首页 > 解决方案 > 尽管我在 where 子句中包含 null,为什么记录从我的左连接中删除

问题描述

我运行以下查询:

create table c.hello as
select a.*, b.timestamp, b.alert 
from nice a
left join bye b 
  on a.number = b.number_nb 
where (Unix_Timestamp(a.time) - Unix_Timestamp(b.timestamp) >= 0)
  and (Unix_Timestamp(a.time) - Unix_Timestamp(b.timestamp) <= 86400) 
   or b.alert_timestamp is null;

为什么我的 hello 表返回的记录比我的 nice 表少?我该如何解决这个问题,我希望保留表中的所有记录。我认为我在 where 子句中的 OR 语句会处理这个问题,不知道为什么它没有。我知道 where 子句将查询转换为内部联接的事实,但是我认为带有 is null 的 OR 子句应该可以解决该问题。你能帮忙吗?

标签: sqldatabasehiveleft-joinhiveql

解决方案


因为您在WHERE子句中放置了一个条件,那就是过滤掉行。

将该条件作为JOIN谓词的一部分,如下所示:

create table c.hello as
select a.*, b.timestamp, b.alert 
from nice a
left join bye b 
  on a.number = b.number_nb 
 and (Unix_Timestamp(a.time) - Unix_Timestamp(b.timestamp) >= 0)
 and (Unix_Timestamp(a.time) - Unix_Timestamp(b.timestamp) <= 86400) 
  or b.alert_timestamp is null;

你看得到差别吗?


推荐阅读