加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 站长学院 > MySql教程 > 正文

MySQL更新错误1093

发布时间:2020-12-11 23:49:24 所属栏目:MySql教程 来源:网络整理
导读:这适用于doc_id是主键的表: select count(*)+1 from doctor where exp (select exp from doctor where doc_id='001');+------------+| count(*)+1 |+------------+| 2 |+------------+ 但是,当我使用相同的选择查询在表中设置字段时,它会报告以下错误: upd

这适用于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

我无法理解它正在讨论哪个目标表引用.谁能解释一下? 最佳答案 此限制记录在MySQL manual中:

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

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

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';

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读