使用 MS Access 中 2 个表的子查询删除重复项

DELETE duplicates with subquery from 2 tables in MS Access

我有两个具有相同结构的 table 可以有重复的记录,我想确定 table 2 中的哪些已经存在于 table 1 中并从 table 2. 以下SELECT returns 重复的记录我要删除。 None 个 table 有一个主键,所以我需要做多个 'ON' 来识别唯一记录。

        SELECT V.*
        FROM table2 AS V
        INNER JOIN table1 AS N
        ON V.column1 = N.column1 AND V.column2 = N.column2 AND V.column3= N.column3;

然后我将其作为 DELETE 的子查询插入:

        DELETE FROM table2
        WHERE table2.column1 IN
        (SELECT V.*
        FROM table2 AS V
        INNER JOIN table1 AS N
        ON V.column1 = N.column1 AND V.column2 = N.column2 AND V.column3= N.column3);

当运行这个查询时我得到以下错误:

You have written a query that can return more than one field without using the reserved word EXISTS in the FROM clause of the main query. Correct the SELECT instruction of the subquery to request a single field.

我也试过这种方式,但是它删除了所有记录 from table 2,不仅是子查询的结果:

        DELETE FROM table2
        WHERE EXISTS
        (SELECT V.*
        FROM table2 AS V
        INNER JOIN table1 AS N
        ON V.column1 = N.column1 AND V.column2 = N.column2 AND V.column3= N.column3);

这是我想出的第一个解决方案,但我想知道在 MS Access 中将 table2 中的所有记录插入到 table1 中是否更容易不匹配,然后删除 table2.

所有建议将不胜感激:)

接受错误消息的建议并尝试使用存在的逻辑:

DELETE
FROM table2 t2
WHERE EXISTS (SELECT 1 FROM table1 t1
              WHERE t1.column1 = t2.column1 AND
                    t1.column2 = t2.column2 AND
                    t1.column3 = t2.column3);

您当前的 exists 尝试存在的问题是 EXISTS 子句内的查询总是有一个结果集,并且该结果集独立于外部 delete 调用。所以,所有记录都被删除了。

我认为您只是缺少子查询中的特定列。

这应该会更好:

DELETE FROM table2
WHERE table2.column1 IN
(SELECT V.column1 
FROM table2 AS V
INNER JOIN table1 AS N
ON V.column1 = N.column1 AND V.column2 = N.column2 AND V.column3= N.column3);