[樂意黎原創]MySQL 中 You can't specify target table 'table_name' for update in FROM clause錯誤解決辦法

如下圖,當執行更新時

update `table_aerchi` as a
    set `biaohao` = (
        select count(*) from `table_aerchi` as b
        where b.`cuncode` ='532523101003' and b.`cunxuhao` ='01'
    )
where a.`cuncode` ='532523101003' and a.`cunxuhao` ='01'

拋錯誤,即: 失敗原因:You can't specify target table 'outer_tb' for update in FROM clause

解決方法:select的結果再通過一箇中間表select多一次,就可以避免這個錯誤

修改如下, table_aerchi 再用一次, 即用  (select * from `table_aerchi`) 來表示 table_aerchi

update `table_aerchi` as a
    set `biaohao` = (
        select count(*) from (select * from `table_aerchi`) as b
        where b.`cuncode` ='532523101003' and b.`cunxuhao` ='01'
    )where a.`cuncode` ='532523101003' and a.`cunxuhao` ='01'

執行後,成功。 提示: 

執行成功,影響行數:[1],耗時:[141ms.]

樂意黎

2019-07-21

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