SELECT
a.foo
b.bar
c.foobar
FROM tableOne AS a
INNER JOIN tableTwo AS b ON a.pk = b.fk
LEFT JOIN tableThree AS c ON b.pk = c.fk
WHERE a.foo = 'something'
AND c.foobar = 'somethingelse'
Having the and clause after the where clause seems to turn the left join into an inner join. The behavior i am seeing is if there isnt ‘somethingelse’ in tableThree there will be 0 rows returned.
If i move c.foobar = ‘somethingelse’ into the join clause the stored join will act like a left join.
SELECT
a.foo
b.bar
c.foobar
FROM tableOne AS a
INNER JOIN tableTwo AS b ON a.pk = b.fk
LEFT JOIN tableThree AS c ON b.pk = c.fk
AND c.foobar = 'somethingelse'
WHERE a.foo = 'something'
Can someone point me at some documentation describing why this happens? THank you very much
It’s because of your
WHEREclause.Whenever you specify a value from the right side of a left join in a
WHEREclause (which isNOT NULL), you necessarily eliminate all of theNULLvalues and it essentially becomes anINNER JOIN.If you write,
AND (c.foobar = 'somethingelse' OR c.foobar IS NULL)that will solve the problem.You can also move the
c.foobarportion into your join predicate, and that too will solve the issue.