How can I implement SQL INTERSECT and MINUS operations in MS Access

前端 未结 5 501
梦谈多话
梦谈多话 2020-12-01 12:20

I have researched and haven\'t found a way to run INTERSECT and MINUS operations in MS Access. Does any way exist

5条回答
  •  小蘑菇
    小蘑菇 (楼主)
    2020-12-01 12:56

    INTERSECT is NOT an INNER JOIN. They're different. An INNER JOIN will give you duplicate rows in cases where INTERSECT WILL not. You can get equivalent results by:

    SELECT DISTINCT a.*
    FROM a
    INNER JOIN b
       on a.PK = b.PK
    

    Note that PK must be the primary key column or columns. If there is no PK on the table (BAD!), you must write it like so:

    SELECT DISTINCT a.*
    FROM a
    INNER JOIN b
       ON  a.Col1 = b.Col1
       AND a.Col2 = b.Col2
       AND a.Col3 = b.Col3 ...
    

    With MINUS, you can do the same thing, but with a LEFT JOIN, and a WHERE condition checking for null on one of table b's non-nullable columns (preferably the primary key).

    SELECT DISTINCT a.*
    FROM a
    LEFT JOIN b
       on a.PK = b.PK
    WHERE b.PK IS NULL
    

    That should do it.

提交回复
热议问题