MYSQL之You can't specify target table for update in FROM clause解决办法

mysql中You can't specify target table for update in FROM clause错误的意思是说,

不能先select出同一表中的某些值,再update这个表(在同一语句中)。


比如这种语句:

update test set name = "陈帅1" where id =(select max(id) from test)

delete from test where id = (select max(id) from test)

原因是: 更新和删除语句的where不能和子查询是同一个表

https://dev.mysql.com/doc/refman/8.0/en/delete.html

You cannot delete from a table and select from the same table in a subquery.


https://dev.mysql.com/doc/refman/8.0/en/update.html

You cannot update a table and select from the same table in a subquery.

tags: Mysql