MySQL select rows where left join is null

后端 未结 6 1385
暗喜
暗喜 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

    Here is a query that returns only the rows where no correspondance has been found in both columns user_one and user_two of table2:

    SELECT T1.*
    FROM table1 T1
    LEFT OUTER JOIN table2 T2A ON T2A.user_one = T1.id
    LEFT OUTER JOIN table2 T2B ON T2B.user_two = T1.id
    WHERE T2A.user_one IS NULL
        AND T2B.user_two IS NULL
    

    There is one jointure for each column (user_one and user_two) and the query only returns rows that have no matching jointure.

    Hope this will help you.

提交回复
热议问题