LEFT OUTER JOIN query not returning expected rows

前端 未结 2 2015
死守一世寂寞
死守一世寂寞 2020-12-06 18:32

My aim is to do exactly what a LEFT OUTER JOIN intends to do using the 4th venn diagram: SQL Diagrams:

\"enter

相关标签:
2条回答
  • 2020-12-06 18:38

    Use

    SELECT * 
       FROM   consultant_memberships 
           LEFT  Outer JOIN consultant_memberships_list 
                    ON consultant_memberships_list.membership_id = consultant_memberships.`id`  
                    and  consultant_memberships_list.consultant_id = 5 
     where  consultant_memberships_list.membership_id IS NULL;
    

    The Where clause used before in your query "consultant_memberships_list.consultant_id = 5 " was neglecting the left outer join.

    0 讨论(0)
  • 2020-12-06 19:03

    You current query is basically doing an INNER JOIN because of the consultant_id = 5 on the WHERE clause. I believe you actually want to use:

    SELECT * 
    FROM   consultant_memberships m
    LEFT OUTER JOIN consultant_memberships_list l
      ON m.`id` = l.membership_id 
      AND l.consultant_id = 5 
    WHERE l.membership_id IS NULL;
    

    See SQL Fiddle with Demo

    0 讨论(0)
提交回复
热议问题