MySQL更新错误1093

5 mysql sql mysql-error-1093 sql-update

这适用于doc_id主表中的表:

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

+------------+
| count(*)+1 |
+------------+
|          2 |
+------------+
Run Code Online (Sandbox Code Playgroud)

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

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
Run Code Online (Sandbox Code Playgroud)

我无法理解它正在讨论哪个目标表引用.谁能解释一下?

Ike*_*ker 10

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';
Run Code Online (Sandbox Code Playgroud)

  • 虽然这让我想知道,这种限制是否应该保护你免受变通方案揭示的或者是技术问题的影响? (3认同)