打开APP
userphoto
未登录

开通VIP,畅享免费电子书等14项超值服

开通VIP
update mysql row (You can't specify target table 'x' for update in FROM clause)

sql语句(update/delete都会出现此问题)

update x set available_material_id = null where id not in (select id from x where additional_info = 1);

mistake

大致意思是,在同一语句中,不能先select出同一表中的某些值,再update这个表。

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

mysql5.7解决办法

update x left join
       x xx
       on x.id = xx.id and xx.additional_info = 1
    set available_material_id = null
    where xx.id is null;

老办法(有人说5.7已经不能用了)

原始:

DELETE FROM tempA WHERE tid IN (
SELECT MAX(tid) AS tid FROM tempA GROUP BY name,age
)

改造后

DELETE FROM tempA WHERE tid NOT IN (
SELECT t.tid FROM (
SELECT MAX(tid) AS tid FROM tempA GROUP BY name,age
) t
)

查询的时候增加一层中间表,就可以避免该错误。

参考

https://stackoverflow.com/questions/51087937/on-update-mysql-row-you-cant-specify-target-table-x-for-update-in-from-claus
https://blog.csdn.net/h996666/article/details/81699255
https://stackoverflow.com/questions/4429319/you-cant-specify-target-table-for-update-in-from-clause/14302701  
https://www.cnblogs.com/pcheng/p/4950383.html  
https://blog.csdn.net/poetssociety/article/details/82391523
本站仅提供存储服务,所有内容均由用户发布,如发现有害或侵权内容,请点击举报
打开APP,阅读全文并永久保存 查看更多类似文章
猜你喜欢
类似文章
MySQL 中 You can''''t specify target table ''''表名'''' for update in FROM clause错误解决办法
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 For Update行级锁定
MySQL执行update时的[ERROR 1093]处理方法
MySQL 触发器
更多类似文章 >>
生活服务
热点新闻
分享 收藏 导长图 关注 下载文章
绑定账号成功
后续可登录账号畅享VIP特权!
如果VIP功能使用有故障,
可点击这里联系客服!

联系客服