MySQL left outer join with where clause - return unmatched rows

前端 未结 1 1136
执笔经年
执笔经年 2020-12-30 04:26

I have two tables: pq and pe. I am trying to LEFT OUTER JOIN left table (pq) on right table (pe

相关标签:
1条回答
  • 2020-12-30 05:08

    Yes. The where clause is turning the left outer join into an inner join.

    Why? The value of pe.pqid is NULL (as is pe.uid) when there is no match. So the comparison in the where clause fails (almost all comparisons to NULL return NULL which is considered false).

    The solution is to move the comparison to the on clause:

    SELECT pq.id, pq.data, pe.data
    FROM pq LEFT OUTER JOIN
         pe
         ON pq.id = pe.pqid and
            pe.uid='12345'
    ORDER BY pq.id LIMIT 2
    
    0 讨论(0)
提交回复
热议问题