Well, this is the order of operations.
SELECT t1.* FROM table1 t1 INNER JOIN table2 t2 LEFT JOIN table3 t3 ON t3.table3_ID = t2.table2_ID ON t2.table2_ID = t1.table1_ID
can be rewritten as:
SELECT t1.* FROM table1 t1 -- inner join t1 INNER JOIN (table2 t2 LEFT JOIN table3 t3 ON t3.table3_ID = t2.table2_ID) -- with this ON t2.table2_ID = t1.table1_ID -- on this condition
So, first you LEFT JOIN t2 with t3, based on the join condition: table3_ID = table2_ID, then you INNER JOIN t1 with t2 on table2_ID = table1_ID.
In your second example, you first INNER JOIN t1 with t2, and then LEFT JOIN the resulting internal join to table t3 in the condition table2_ID = table1_ID.
SELECT t1.* FROM table1 t1 INNER JOIN table2 t2 ON t2.table2_ID = t1.table1_ID LEFT JOIN table3 t3 ON t3.table3_ID = t2.table2_ID
can be rewritten as:
SELECT t1.* FROM (table1 t1 INNER JOIN table2 t2 ON t2.table2_ID = t1.table1_ID)
EDIT
I apologize. My first comment was wrong. These two queries will give the same results, but there may be a difference in performance, since the first query can run slower than the second query in some cases (when table 1 contains only a subset of the elements in table 2), because the LEFT JOIN will be executed first - and only then intersects with table 1. Unlike the second query, which allows the query optimizer to do this work.
Mike dinescu
source share