删除 Access 中不匹配的记录

发布于 2024-10-30 22:35:46 字数 575 浏览 1 评论 0原文

我在 Access 数据库中有一个表,其中的记录可以从其他两个表中的任何一个引用。如果其他任何一个表都没有引用它们,我需要从该表中删除记录。 到目前为止,我最好的解决方案是创建一个子查询来返回引用记录的 id 并从删除查询中引用子查询。 (子查询必须是单独的,因为 Access 不允许在嵌套子查询中使用 UNION。)

所以 ...
SelectQuery:

SELECT TableB.id FROM TableB INNER JOIN TableA ON TableB.id = TableA.id  
UNION  
SELECT TableC.id FROM TableC INNER JOIN TableA ON TableC.id = TableA.id  

DeleteQuery:

 DELETE * FROM TableA WHERE id NOT IN (SELECT * FROM SelectQuery)

这太慢了......一定有更好的方法吗?

我试图避免向 TableA 添加布尔“Used”字段...

I have a table in an Access database where records may be referenced from either of two other tables. I need to delete records from that table if they are not referenced by either of the others.
My best solution so far has been to create a subquery to return the id's of the referenced records and to refer to the subquery from a delete query. (The subquery has to be separate because Access does not allow UNION in nested subqueries.)

So ...
SelectQuery:

SELECT TableB.id FROM TableB INNER JOIN TableA ON TableB.id = TableA.id  
UNION  
SELECT TableC.id FROM TableC INNER JOIN TableA ON TableC.id = TableA.id  

DeleteQuery:

 DELETE * FROM TableA WHERE id NOT IN (SELECT * FROM SelectQuery)

This is excruciatingly slow ... there must be a better way?

I was trying to avoid having to add a boolean 'Used' field to TableA ...

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

情丝乱 2024-11-06 22:35:46

@Matthew PK 建议使用两个 NOT IN 子查询,这在理论上是一个好主意,但正如我在评论中观察到的那样,NOT IN 和 NOT EXISTS 被 Jet/ACE 优化得很差,并且通常不会使用比较两侧的索引。我想知道子查询是否有必要:

  DELETE *
  FROM (TableA LEFT JOIN TableB ON TableA.ID = TableB.ID) LEFT JOIN TableC ON TableA.ID = TableC.ID
  WHERE TableB.ID Is Null AND TableC.ID Is Null;

这肯定会使用您的索引。如果需要子查询,可以将TableB和TableC替换为相关的子查询。

@Matthew PK suggests using two NOT IN subqueries, which is theoretically a good idea, but as I observed in a comment, NOT IN and NOT EXISTS are poorly optimized by Jet/ACE and will often not use the indexes on both sides of the comparison. I'm wondering whether or not subqueries are necessary or not:

  DELETE *
  FROM (TableA LEFT JOIN TableB ON TableA.ID = TableB.ID) LEFT JOIN TableC ON TableA.ID = TableC.ID
  WHERE TableB.ID Is Null AND TableC.ID Is Null;

This would definitely use your indexes. If a subquery is necessary, you could replace TableB and TableC with the relevant subqueries.

允世 2024-11-06 22:35:46

为什么不是这样的:

DELETE FROM TableA 
WHERE 
    id NOT IN (SELECT id FROM TableB)
AND
    id NOT IN (SELECT id FROM TableC)

Why not something like this:

DELETE FROM TableA 
WHERE 
    id NOT IN (SELECT id FROM TableB)
AND
    id NOT IN (SELECT id FROM TableC)

?

故人的歌 2024-11-06 22:35:46

是否可以根据您的 SelectQuery 创建一个新表,删除原始表,然后将新表重命名为原始名称?

Is it acceptable to create a new table based on your SelectQuery, delete the original table, and rename the new one to the original name?

握住我的手 2024-11-06 22:35:46
/* delete more records */

DELETE FROM table1
WHERE NOT EXISTS
(SELECT field FROM table2 WHERE table2.field = table1.field)
/* delete more records */

DELETE FROM table1
WHERE NOT EXISTS
(SELECT field FROM table2 WHERE table2.field = table1.field)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文