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