Mysql 抛出错误

发布于 2024-10-02 07:08:26 字数 180 浏览 6 评论 0原文

DELETE FROM mytable WHERE id IN (SELECT id FROM mytable where roll=1)

我有一个表mytable。我的上述查询抛出错误。

您无法在 FROM 子句中指定要更新的目标表“mytable”

DELETE FROM mytable WHERE id IN (SELECT id FROM mytable where roll=1)

I have a table mytable. My above query is throwing an error.

You can't specify target table 'mytable' for update in FROM clause

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

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

发布评论

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

评论(6

仄言 2024-10-09 07:08:26

来自 MySQL 文档

目前,您无法在子查询中从表中删除并从同一个表中进行选择。

幸运的是,您不需要子查询。就这样做:

DELETE FROM mytable WHERE roll=1

启动起来更短、更清晰。

From the MySQL documentation:

Currently, you cannot delete from a table and select from the same table in a subquery.

Fortunately, you don't need the subquery. Just do:

DELETE FROM mytable WHERE roll=1

It's much shorter and clearer to boot.

夏末染殇 2024-10-09 07:08:26

你为什么不这样做呢?

DELETE FROM mytable WHERE roll=1

Why don't you just do this?

DELETE FROM mytable WHERE roll=1
如痴如狂 2024-10-09 07:08:26

你为什么不直接写呢

DELETE FROM mytable WHERE roll=1


发生该错误的原因是,当上级查询修改同一个表时,MySql 不喜欢从子查询中的表中获取数据。

Why don't you just write

DELETE FROM mytable WHERE roll=1

?
The error occurs because MySql doesn't like it to fetch form a table in a subquery when the superior query modifies the same table.

绝不放开 2024-10-09 07:08:26

为什么不简单地:

DELETE FROM mytable WHERE roll=1

Why not simply:

DELETE FROM mytable WHERE roll=1
鱼窥荷 2024-10-09 07:08:26

你为什么使用子查询?你可以这样写:

DELETE FROM mytable WHERE where roll=1;

Why are you using subquery? You can write it this way:

DELETE FROM mytable WHERE where roll=1;
萌辣 2024-10-09 07:08:26

此帖子解释了为什么您不能执行此操作。

在这个例子中,你可以使用:

DELETE FROM mytable WHERE roll=1

This thread explains why you can't do this.

And in this example, you can just use:

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