A subsequent inner join will only "essentially invalidate" an outer join if the inner join's ON
clause requires should-be-optional rows to be present. In such a case, reordering the join either won't work or won't help; rather, the only fix is to change the inner join to an appropriate outer join.
So, for example, this works fine:
SELECT *
FROM person
LEFT JOIN address
ON person.address_id = address.id
INNER JOIN email
ON person.email_id = email.id
and is equivalent to what you'd get if you moved the left outer join (lines 3–4) after the inner join (lines 5–6); whereas this does not work as intended:
SELECT *
FROM person
LEFT JOIN address
ON person.address_id = address.id
INNER JOIN city
ON address.city_id = city.id
because the second ON
clause can only be satisfied when address.city_id
is non-null. (In this case the right fix is to change the inner join to a left outer join.)
That said, I do agree with Gordon Linoff that it's usually best to put your inner joins before your left outer joins; this is because inner joins tend to indicate more "essential" restrictions, so this ordering is usually more readable. (And I agree with both Gordon Linoff and Shawn that right outer joins are usually better avoided.)
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…