【Mysql】"You can't specify target table 'XX' for update in FROM clause"解決方法

1.問題描述

在聯合兩張表進行更新的時候,出現了以下問題:

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

大意是: 不能在from子句中指定表“xx”,一般這個XX是需要更新的表名。

2.解決方法

來源stackoverflow的解決方法如下:

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

應該如下:

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;

https://stackoverflow.com/questions/51087937/on-update-mysql-row-you-cant-specify-target-table-x-for-update-in-from-claus

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章