使用 JOIN 时如何在 MS Access 中删除?

发布于 2024-10-30 18:17:06 字数 485 浏览 0 评论 0原文

我尝试在 MS Access 中使用 DELETE 子句,但在使用 JOIN 子句时遇到问题。我注意到这可以通过使用 DISTINCTROW 关键字来完成。

例如,以下 SQL 语句不允许删除:

DELETE Table1.*
FROM Table1 INNER JOIN Table2 ON Table1.Name=Table2.Name;

但是,此语句允许:

DELETE DISTINCTROW Table1.*
FROM Table1 INNER JOIN Table2 ON Table1.Name=Table2.Name;
  • 为什么使用 DISTINCTROW 关键字时 DELETE 可以工作?
  • 更具体地说,JET 引擎中发生了什么需要这样做?

I am attempting to use the DELETE clause in MS Access and have an issue when also using the JOIN clause. I have notice this can be accomplished by using the DISTINCTROW key word.

For example, the following SQL statement does not allow for deletion:

DELETE Table1.*
FROM Table1 INNER JOIN Table2 ON Table1.Name=Table2.Name;

However, this statement does:

DELETE DISTINCTROW Table1.*
FROM Table1 INNER JOIN Table2 ON Table1.Name=Table2.Name;
  • Why does the DELETE work when using the DISTINCTROW key word?
  • More specifically, what is happening in the JET engine to require this?

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

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

发布评论

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

评论(5

皇甫轩 2024-11-06 18:17:06
Delete Table1.*
From Table1
Where Exists( Select 1 From Table2 Where Table2.Name = Table1.Name ) = True

为了扩展我的答案,官方 SQL 规范没有专门规定在操作查询中使用联接,因为它可能会产生不明确的结果。因此,如果您可以避免像我在这里那样在操作查询中使用联接,那就更好了(而且 Access 更高兴)。 Access 需要 DISTINCTROW 的原因是,两个表之间的联接可能会创建 Table1 行的重复项(即,Table2 中有多个相关行),从而 Access 会感到困惑。我还发现,如果您尝试使用 Join 并且主键不存在,Access 将犹豫不决。一般来说,如果可以的话,最好避免在操作查询中进行联接。

Delete Table1.*
From Table1
Where Exists( Select 1 From Table2 Where Table2.Name = Table1.Name ) = True

To expand on my answer, the official SQL specification does not provide for using Joins in action queries specifically because it can create ambiguous results. Thus, it is better (and Access is much happier) if you can avoid using Joins in action queries like I have here. The reason that Access wants DISTINCTROW is that it is likely that the Join between the two tables would create duplicates of Table1 rows (i.e., there are multiple related rows in Table2) and thus Access gets confused. I've also found that if you try to use a Join and a primary key does not exist Access will balk. In general, it is better to avoid a join in an action query if you can.

遮云壑 2024-11-06 18:17:06

需要注意的一个问题:这不适用于表/查询别名!

DELETE a.*
from tblA as A
where exists (select 1 from tblB as B where a.id=b.id)

删除 tblA 中的所有记录!我分别使用 tblA 和 tblB 的别名进行了尝试 - 相同的结果(Access 2010)。

SELECT 也会发生这种情况(我经常在删除之前使用它)...

One problem to be aware of: This does NOT work with table/query aliases!

DELETE a.*
from tblA as A
where exists (select 1 from tblB as B where a.id=b.id)

Deletes ALL records in tblA! I tried it using alias for tblA and tblB seperately - same result (Access 2010).

Happens with SELECT as well (which I often use before deleting)...

似狗非友 2024-11-06 18:17:06
DELETE a.*
FROM tblA AS A
WHERE EXISTS (SELECT 1 FROM tblB AS B WHERE a.id=b.id)

试试这个

DELETE tblA 
FROM tblB  
WHERE EXISTS (SELECT * FROM tblA AS A,tblB AS B WHERE A.id=B.id)
DELETE a.*
FROM tblA AS A
WHERE EXISTS (SELECT 1 FROM tblB AS B WHERE a.id=b.id)

try this

DELETE tblA 
FROM tblB  
WHERE EXISTS (SELECT * FROM tblA AS A,tblB AS B WHERE A.id=B.id)
若言繁花未落 2024-11-06 18:17:06

只需将查询的属性设置为 Unique Records YES 即可。

Just set the property of query to Unique Records YES and it will work.

望她远 2024-11-06 18:17:06

尝试使用这个:

DELETE Table.* FROM Table
WHERE ID IN (SELECT ID FROM TableOrQuery2)

Try using this:

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