前些天發現了一個巨牛的人工智能學習網站,通俗易懂,風趣幽默,忍不住分享一下給大家。點擊跳轉到教程。
報錯如題,意思大致是:在一條 sql 語句中不能先查出來部分內容,再同時又對當前表作修改。
解決方法:給查詢加別名,用中間表來實現不是對同一表作操作。
如錯誤定法:
UPDATE xxx_department
SET rank = '2'
WHERE aaa_id in (SELECT id FROM xxx_department WHERE aaa_id IS NULL) ;
改后的寫法:
UPDATE xxx_department
SET rank = '2'
WHERE aaa_id in (SELECT * FROM (SELECT id FROM xxx_department WHERE aaa_id IS NULL) a );
?
----------------- 我 是 傻 傻 的 分 隔 線 -----------------
2019. 4.3 后記 :
今天在評論中看到另一方法,特別記錄一下。(再度感謝這位愿意在評論中分享解決方法的朋友)
-- sql 原寫法:update x set xxx_xxrial_id = null where id not in (select id from x where ccc_info = 1);-- 使用 left join 改寫為自鏈接方式:update x left joinx aon x.id = a.id and a.ccc_info = 1set xxx_xxrial_id = nullwhere a.id is null;
此方法出處:https://stackoverflow.com/questions/51087937/on-update-mysql-row-you-cant-specify-target-table-x-for-update-in-from-claus