使用 JOIN 时如何在 MS Access 中删除?
我尝试在 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 theDISTINCTROW
key word? - More specifically, what is happening in the JET engine to require this?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
为了扩展我的答案,官方 SQL 规范没有专门规定在操作查询中使用联接,因为它可能会产生不明确的结果。因此,如果您可以避免像我在这里那样在操作查询中使用联接,那就更好了(而且 Access 更高兴)。 Access 需要 DISTINCTROW 的原因是,两个表之间的联接可能会创建 Table1 行的重复项(即,Table2 中有多个相关行),从而 Access 会感到困惑。我还发现,如果您尝试使用 Join 并且主键不存在,Access 将犹豫不决。一般来说,如果可以的话,最好避免在操作查询中进行联接。
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.
需要注意的一个问题:这不适用于表/查询别名!
删除 tblA 中的所有记录!我分别使用 tblA 和 tblB 的别名进行了尝试 - 相同的结果(Access 2010)。
SELECT 也会发生这种情况(我经常在删除之前使用它)...
One problem to be aware of: This does NOT work with table/query aliases!
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)...
试试这个
try this
只需将查询的属性设置为 Unique Records YES 即可。
Just set the property of query to Unique Records YES and it will work.
尝试使用这个:
Try using this: