MySql中You can't specify target table for update in FROM clause

MySql中You can't specify target table for update in FROM clause

问题描述:当我执行下面这段语句时,出现了这个bug

UPDATE account set status=1 where id in ( 
select id from account where ISNULL(status));

出现bug:You can't specify target table for update in FROM clause

造成原因:不能先select出同一表中的某些值,再update这个表(在同一语句中),意思是不能将account 查出来的字段当做update的where条件。

解决方法:在select外边套一层,让数据库认为你不是查同一表的数据作为同一表的更新数据。

UPDATE account set status=1 where id in (
select temp.id from 
(select id from account where ISNULL(status)) temp);

注:该错误只有Mysql会出现,MSSQL和Oracle不会出现该错误

posted @   啊俊同学  阅读(87)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 阿里最新开源QwQ-32B,效果媲美deepseek-r1满血版,部署成本又又又降低了!
· SQL Server 2025 AI相关能力初探
· 单线程的Redis速度为什么快?
· AI编程工具终极对决:字节Trae VS Cursor,谁才是开发者新宠?
· 开源Multi-agent AI智能体框架aevatar.ai,欢迎大家贡献代码
点击右上角即可分享
微信分享提示