MySQL select rows where left join is null

后端 未结 6 1377
暗喜
暗喜 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:15

    One of the best approach if you do not want to return any columns from table2 is to use the NOT EXISTS

    SELECT  table1.id 
    FROM    table1 T1
    WHERE
      NOT EXISTS (SELECT *
                  FROM table2 T2
                  WHERE T1.id = T2.user_one
                      OR T1.id = T2.user_two)
    

    Semantically this says what you want to query: Select every row where there is no matching record in the second table.

    MySQL is optimized for EXISTS: It returns as soon as it finds the first matching record.

    0 讨论(0)
  • 2020-12-08 10:18
    SELECT table1.id 
    FROM table1 
    LEFT JOIN table2 ON table1.id = table2.user_one
    WHERE table2.user_one is NULL
    
    0 讨论(0)
  • 2020-12-08 10:22

    Try following query:-

     SELECT table1.id 
     FROM table1 
     where table1.id 
     NOT IN (SELECT user_one
             FROM Table2
                 UNION
             SELECT user_two
             FROM Table2)
    

    Hope this helps you.

    0 讨论(0)
  • 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.

    0 讨论(0)
  • 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

    0 讨论(0)
  • 2020-12-08 10:28

    You could use the following query:

    SELECT  table1.id 
    FROM    table1 
            LEFT JOIN table2 
                ON table1.id IN (table2.user_one, table2.user_two)
    WHERE   table2.user_one IS NULL;
    

    Although, depending on your indexes on table2 you may find that two joins performs better:

    SELECT  table1.id 
    FROM    table1 
            LEFT JOIN table2 AS t1
                ON table1.id = t1.user_one
            LEFT JOIN table2 AS t2
                ON table1.id = t2.user_two
    WHERE   t1.user_one IS NULL
    AND     t2.user_two IS NULL;
    
    0 讨论(0)
提交回复
热议问题