mysql 中 You can't specify target table for update in FROM clause 錯誤解決方法

You can’t specify target table tbl for update in FROM clause

在mysql中當在一句sql中先後執行對同一張表的select 和 update 或 delete 操作時,就會報錯:
You can't specify target table tbl for update in FROM clause

例如:

update tbl as a set a.col = 
(
    select b.col from tbl as b where b.id = 1
);

解決方法: 將select字句中的表再嵌套一層select

update tbl as a set a.col = 
(
    select c.col from 
    (
        select * from tbl as b where b.id = 1
    ) 
    as c
);

或者:

update tbl as a inner join tbl as b set a.col = value where conditions 
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章