Regardless of whether you are using JOIN
or LEFT JOIN
, use ON
to specify how the tables are related and use WHERE
to filter.
In the case of JOIN
, the it does not matter where you put the filtering; it is for readability that you should follow the above rule.
In the case of LEFT JOIN
, the results are likely to be different.
If you do
EXPLAIN EXTENDED SELECT ...
SHOW WARNINGS;
you can see what the SQL parser decided to do. In general, it moves ON
clauses are to WHERE
, indicating that it does not matter (to the semantics) which place they are. But, for LEFT JOIN
, some things must remain in the ON
.
Note another thing:
FROM a ...
LEFT JOIN b ...
WHERE b.foo = 123
effectively throws out the LEFT
. The difference between LEFT
and non-LEFT
is whether you get rows of b
filled with NULLs
. But WHERE b.foo = 123
says you definitely do not want such rows. So, for clarity for the reader, do not say LEFT
.
So, I agree with your original formulation. But I also like short aliases for all tables. Be sure to qualify all columns -- the reader may not know which table a column is in.
Your title says "multiple" joins. I discussed a single JOIN
; the lecture applies to any number of JOINs
.