MySQL 更新错误 1093

发布于 2024-10-22 03:15:07 字数 554 浏览 4 评论 0原文

这适用于以 doc_id 为主键的表:

select count(*)+1 from doctor where 
exp > (select exp from doctor where doc_id='001');

+------------+
| count(*)+1 |
+------------+
|          2 |
+------------+

但是当我使用相同的选择查询来设置表中的字段时,它会报告以下错误:

update doctor set rank=
(  select count(*)+1 from doctor where 
   exp > (select exp from doctor where doc_id='001')
) where doc_id='001';

ERROR 1093 (HY000): You can't specify target table 'doctor' for update 
in FROM clause

我无法理解哪个目标它正在谈论的表参考。有人可以解释一下吗?

This works in a table where doc_id is the primary key:

select count(*)+1 from doctor where 
exp > (select exp from doctor where doc_id='001');

+------------+
| count(*)+1 |
+------------+
|          2 |
+------------+

But when I'm using the same select query to set a field in the table, it reports the following error:

update doctor set rank=
(  select count(*)+1 from doctor where 
   exp > (select exp from doctor where doc_id='001')
) where doc_id='001';

ERROR 1093 (HY000): You can't specify target table 'doctor' for update 
in FROM clause

I can't understand which target table reference it is talking of. Can someone explain?

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

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

发布评论

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

评论(3

何时共饮酒 2024-10-29 03:15:07

此限制记录在 MySQL 手册中:

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

作为解决方法,您可以将子查询包装在另一个子查询中并避免该错误:

update doctor set rank=
(select rank from (  select count(*)+1 as rank from doctor where 
   exp > (select exp from doctor where doc_id='001')
) as sub_query) where doc_id='001';

This restriction is documented in the MySQL manual:

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

As a workaround, you can wrap the sub-query in another sub-query and avoid that error:

update doctor set rank=
(select rank from (  select count(*)+1 as rank from doctor where 
   exp > (select exp from doctor where doc_id='001')
) as sub_query) where doc_id='001';
烟酉 2024-10-29 03:15:07

您不能在 from 子句的子查询中使用要更新的表。尝试连接或双子查询:)

You can't use a table you're updating in a subquery in the from clause. Try a join, or a double subquery :)

五里雾 2024-10-29 03:15:07

我认为这可能是因为您在同一张表上读取和写入。这可能是阻止写入的预防性方法,因为您的更新可能会影响正在读取的数据。

您可能需要将子查询分离为使用中间虚拟表。

I think it might be because you're reading and writing from the same table. This could be a preventive way of blocking writing because your update could affect the data being read.

You might need to separate the subquery into using an intermediate virtual table.

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