未加入完整 table

Not joining full table

我需要 LEFT JOIN 但 "right" table 非常大,我只需要 2018-01-01 之后的值,而整个 table 的记录最多2012 年。因此,为了加快我的查询速度,我不需要 LEFT JOIN 完整的 table,而是记录 > 2018-01-01 的 table。如果我在查询末尾使用 where 语句,它仍将使用完整的 table 对吗?我该怎么做?

您可以将 where 过滤器放在联接中。

select * from tableA a
left Join tableB b on a.id=b.id and b.date > 2018-01-01
where a.field =''

将约束放在 where 子句中与放在 on 子句中的行为不同。

原因是先执行outer join,然后是where子句。查询中任何在可选 table 中没有匹配项的行,日期列将为空。接下来应用您的 where 子句,并且由于 null <> anything,您将删除所有这些行。基本上,您会将外部联接变成内部联接。这就是为什么您想在 on 子句中对可选的 table 施加任何限制,如 Dave Kelly 的回答所示。