sql - 尽管我在 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 子句应该可以解决该问题。你能帮忙吗?
解决方案
因为您在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;
你看得到差别吗?
推荐阅读
- javascript - 即使调用了函数也不会触发
- java - 逆向工程 JDBC Oracle 连接字符串
- docker - 无法在安装了 redis 的 docker 容器中使用 redis-cli
- makefile - make: SECONDEXPANSION 的范围
- c++ - 静态对象的析构函数不(总是)用 LD_PRELOAD 调用
- python - 如何创建自动编码器的解码器部分?
- java - 如何每天显示不同的图像
- webstorm - 如何忽略 WebStorm 中拼写检查器中的 lorem ipsum 文本?
- python - 如何从垂直列表中创建变量
- r - 如何创建一个列来指示第 1 列中的每个值是否出现在第 2、3、4 列等中?