MySQL select rows where left join is null

后端 未结 6 1379
暗喜
暗喜 2020-12-08 10:02

I have these MySQL tables:

table1:

id | writer
1  | Bob   
2  | Marley
3  | Michael

table2:

user_one | user_two
            


        
6条回答
  •  無奈伤痛
    2020-12-08 10:26

    Try:

    SELECT A.id FROM
    (
      SELECT table1.id FROM table1 
      LEFT JOIN table2 ON table1.id = table2.user_one
      WHERE table2.user_one IS NULL
    ) A
    JOIN (
      SELECT table1.id FROM table1 
      LEFT JOIN table2 ON table1.id = table2.user_two
      WHERE table2.user_two IS NULL
    ) B
    ON A.id = B.id
    

    See Demo

    Or you could use two LEFT JOINS with aliases like:

    SELECT table1.id FROM table1 
     LEFT JOIN table2 A ON table1.id = A.user_one
     LEFT JOIN table2 B ON table1.id = B.user_two
     WHERE A.user_one IS NULL
     AND B.user_two IS NULL
    

    See 2nd Demo

提交回复
热议问题