You can‘t specify target table ‘XXX‘ for update in FROM clause
生活随笔
收集整理的這篇文章主要介紹了
You can‘t specify target table ‘XXX‘ for update in FROM clause
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
前些天發現了一個巨牛的人工智能學習網站,通俗易懂,風趣幽默,忍不住分享一下給大家。點擊跳轉到教程。
報錯如題,意思大致是:在一條 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
總結
以上是生活随笔為你收集整理的You can‘t specify target table ‘XXX‘ for update in FROM clause的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: List和DataTable的Limit
- 下一篇: 博客园博客PDF生成器