mysqlsqljoinleft-joinon-clause

MySQL placement of conditions in on-clauses of multiple outer joins


As a follow up to In SQL / MySQL, what is the difference between "ON" and "WHERE" in a join statement? and SQL join: where clause vs. on clause - it does matter if a condition is placed in the on-clause vs. the where-clause in an outer join.

However, does it matter which on-clause the condition is placed in when there are multiple outer joins?

For example, could these produce different results?

select * from t1 left join t2 on t1.fid=t2.id and t2.col=val
                 left join t3 on t2.fid=t3.id;

vs:

select * from t1 left join t2 on t1.fid=t2.id
                 left join t3 on t2.fid=t3.id and t2.col=val;

Solution

  • Absolutely they are different.

    The fisrt query will only have t2 rows that satisfy t2.col=val

    The second query will include all t2 rows and only list t3 when t2.col=val