left join turns into inner join

后端 未结 8 800
爱一瞬间的悲伤
爱一瞬间的悲伤 2020-11-29 06:24
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.foo         


        
8条回答
  •  伪装坚强ぢ
    2020-11-29 07:19

    It's because of your WHERE clause.

    Whenever you specify a value from the right side of a left join in a WHERE clause (which is NOT NULL), you necessarily eliminate all of the NULL values and it essentially becomes an INNER JOIN.

    If you write, AND (c.foobar = 'somethingelse' OR c.foobar IS NULL) that will solve the problem.

    You can also move the c.foobar portion into your join predicate, and that too will solve the issue.

提交回复
热议问题