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.

*文章为作者原创或网络转载
转载此文请于文首标明作者姓名,保持文章完整性,并请附上出处
未按照规范转载者,陈帅同学保留追究相应责任的权利
编程改变世界

干了这碗鸡汤