执行以下语句:
delete from titles_test
where id not in(
select min(id)
from titles_test
group by emp_no
);
报出错误:

SQL_ERROR_INFO: "You can't specify target table 'titles_test' for update in FROM clause"
不能先select出同一表中的某些值,再update这个表(在同一语句中)
(MySQL的UPDATE或DELETE中子查询不能为同一张表)
办法:把子查询表添加别名就可以了,如下:
delete from titles_test
where id not in
(select * from (select min(id) from titles_test Group by emp_no ) as a)
本文介绍了在MySQL中遇到的更新子查询错误,如何通过添加别名避免更新同一表的限制,并提供了具体操作方法。

被折叠的 条评论
为什么被折叠?



