Mysql
 sql >> Database >  >> RDS >> Mysql

MySQL seleziona le righe in cui il join sinistro è nullo

Potresti usare la seguente 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;

Anche se, a seconda dei tuoi indici su table2 potresti scoprire che due join funzionano meglio:

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;