MySQL optimized query for not in

前端 未结 3 597
一生所求
一生所求 2021-01-21 02:40

I have an excel with VBA program which connects to MySQL database to retrieve some information. Initially, when the program loads I have the below query.

SELECT          


        
相关标签:
3条回答
  • 2021-01-21 02:56

    Use an outer join

    SELECT A.*
    FROM Table1 a
    LEFT OUTER JOIN (SELECT DISTINCT subject
                FROM Table2) b ON a.id = b.subject
    WHERE b.subject IS NULL
    
    0 讨论(0)
  • 2021-01-21 03:02

    Query:

    SELECT a.*
    FROM Table1 a
    WHERE NOT EXISTS (SELECT subject
                      FROM Table2 b
                      WHERE a.id = b.subject)
    
    0 讨论(0)
  • 2021-01-21 03:11

    Similar to the other offered answer, however the query may perform better with a direct link to the table rather than to an inline view on a SELECT DISTINCT:

    SELECT t1.*
    FROM Table1 t1
    LEFT JOIN Table2 t2 ON t1.id = t2.subject
    WHERE t2.subject IS NULL
    
    0 讨论(0)
提交回复
热议问题