左联接:显示右侧的null
假设我们有2列表
TABLEA TABLEB
1 1 01/03/2022
2 2 01/01/2022
3
此SQL语句:
select id
from tableA
left join tableB on tableA.id = tableB.id
显示:
1 1 01/03/2022
2 2 01/01/2022
3 NUll
不幸的是,当我在右表上添加限制时,行为并不相同。
select id
from tableA
left join tableB on tableA.id = tableB.id
where tableB.date > '01/01/2022'
我想要这样的东西:
1 1 01/03/2022
2 NULL
3 NULL
但是我明白了:
1 1 01/03/2022
仅此而已。我没想到这种行为。有人可以告诉该查询应该是什么吗?
Let's say we have 2 tables
TABLEA TABLEB
1 1 01/03/2022
2 2 01/01/2022
3
This SQL statement:
select id
from tableA
left join tableB on tableA.id = tableB.id
displays:
1 1 01/03/2022
2 2 01/01/2022
3 NUll
Unfortunately, when I add a restriction on the right table, the behaviour is not the same.
select id
from tableA
left join tableB on tableA.id = tableB.id
where tableB.date > '01/01/2022'
I would like something like this :
1 1 01/03/2022
2 NULL
3 NULL
But I get this:
1 1 01/03/2022
That's all. I was not expecting this behavior. Can someone tell what the query should be ?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
当您添加Where子句时,您有效地将左联接更改为内部连接。将条件放在联接谓词中可以保持左联接。
When you added the WHERE clause you effectively changed the LEFT JOIN to an INNER JOIN. Putting the condition in the JOIN predicates keeps the LEFT JOIN as intended.