MySQL - 添加 WHERE 子句时 LEFT JOIN 失败
我有4张表如下; SCHEDULES、SCHEDULE_OVERRIDE、SCHEDULE_LOCATION_OVERRIDES 和 LOCATION
我需要从所有表中返回所有行,因此运行此查询工作正常,为任何不存在的值添加 NULL 值:
SELECT.....
FROM (schedule s LEFT JOIN schedule_override so ON so.schedule_id = s.id)
LEFT JOIN schedule_location_override slo ON slo.schedule_override_id = so.id
LEFT JOIN location l ON slo.location_id = l.id
ORDER BY s.id, so.id, slo.id, l.id
然后我需要限制 Schedule_override end_date 字段的结果。我的问题是,一旦我这样做,就根本不会返回 SCHEDULE 表的结果。我需要返回所有时间表,即使不满足覆盖 end_date 标准。
这就是我正在使用的:
SELECT.....
FROM (schedule s LEFT JOIN schedule_override so ON so.schedule_id = s.id)
LEFT JOIN schedule_location_override slo ON slo.schedule_override_id = so.id
LEFT JOIN location l ON slo.location_id = l.id
WHERE so.end_date > '2011-01-30' OR so.end_date IS NULL
ORDER BY s.id, so.id, slo.id, l.id
感谢任何想法/评论。
最好的问候,本。
I have 4 tables as follows; SCHEDULES, SCHEDULE_OVERRIDE, SCHEDULE_LOCATION_OVERRIDES and LOCATION
I need to return ALL rows from all tables so running this query works fine, adding NULL values for any values that are not present:
SELECT.....
FROM (schedule s LEFT JOIN schedule_override so ON so.schedule_id = s.id)
LEFT JOIN schedule_location_override slo ON slo.schedule_override_id = so.id
LEFT JOIN location l ON slo.location_id = l.id
ORDER BY s.id, so.id, slo.id, l.id
I then need to restict results on the schedule_override end_date field. My problem is, as soon as I do this, no results for the SCHEDULE table are returned at all. I need all schedules to be returned, even if the overrides end_date criteria is not met.
Heres what I am using:
SELECT.....
FROM (schedule s LEFT JOIN schedule_override so ON so.schedule_id = s.id)
LEFT JOIN schedule_location_override slo ON slo.schedule_override_id = so.id
LEFT JOIN location l ON slo.location_id = l.id
WHERE so.end_date > '2011-01-30' OR so.end_date IS NULL
ORDER BY s.id, so.id, slo.id, l.id
Appreciate any thoughts/comments.
Best regards, Ben.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您是否尝试过将其放入
ON
子句中?Have you tried putting it in the
ON
clause?这是外连接中一个很常见的错误。
您需要将限制联接的所有内容放入该表的“ON”部分,否则您实际上是将联接转换为内部联接。
因此,将本例中的 WHERE 子句移至 Schedule_override 的 ON 部分,应该没问题。
That's a quite common mistake with outer Joins.
You need to put everything that limits the Join into the "ON" part for that table, otherwise you are effectively transforming the join to an inner one.
So move the WHERE clause in this case into the ON-part of the schedule_override and you should be fine.
是的,当你离开连接时,可能是找不到一行,并且结果中该字段为NULL。当您在 WHERE 子句中添加条件时,该值必须与该条件匹配,如果为 NULL,则不会匹配。
这应该不是问题,因为您显式检查 NULL,所以我真的不知道为什么这个条件失败,除非它确实返回一个日期,但该日期是在 2011-01- 30.
无论如何,您可以尝试将条件移至连接。它将消除检查 NULL 的需要,尽管它实际上不会产生任何影响。
Yes, when you left join, it could be that a row is not found, and the field is NULL in the result. When you add a condition in the WHERE clause, the value must match that condition, which it won't if it's NULL.
That shouldn't be a problem, because you explicitly check for NULL, so I don't really know why this condition fails, unless it does return a date, but that date is befor 2011-01-30.
Anyway, you could try to move the condition to the join. It will eliminate the need to check for NULL, although it shouldn't make a difference really.