You can‘t specify target table ‘XXX‘ for update in FROM clause
生活随笔
收集整理的這篇文章主要介紹了
You can‘t specify target table ‘XXX‘ for update in FROM clause
小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.
前些天發(fā)現(xiàn)了一個(gè)巨牛的人工智能學(xué)習(xí)網(wǎng)站,通俗易懂,風(fēng)趣幽默,忍不住分享一下給大家。點(diǎn)擊跳轉(zhuǎn)到教程。
報(bào)錯(cuò)如題,意思大致是:在一條 sql 語(yǔ)句中不能先查出來(lái)部分內(nèi)容,再同時(shí)又對(duì)當(dāng)前表作修改。
解決方法:給查詢(xún)加別名,用中間表來(lái)實(shí)現(xiàn)不是對(duì)同一表作操作。
如錯(cuò)誤定法:
UPDATE xxx_department SET rank = '2' WHERE aaa_id in (SELECT id FROM xxx_department WHERE aaa_id IS NULL) ;改后的寫(xiě)法:
UPDATE xxx_department SET rank = '2' WHERE aaa_id in (SELECT * FROM (SELECT id FROM xxx_department WHERE aaa_id IS NULL) a );?
----------------- 我 是 傻 傻 的 分 隔 線(xiàn) -----------------
2019. 4.3 后記 :
今天在評(píng)論中看到另一方法,特別記錄一下。(再度感謝這位愿意在評(píng)論中分享解決方法的朋友)
-- sql 原寫(xiě)法:update x set xxx_xxrial_id = null where id not in (select id from x where ccc_info = 1);-- 使用 left join 改寫(xiě)為自鏈接方式: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
總結(jié)
以上是生活随笔為你收集整理的You can‘t specify target table ‘XXX‘ for update in FROM clause的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: List和DataTable的Limit
- 下一篇: 博客园博客PDF生成器