mysql中You can't specify target table for update in FROM clause錯(cuò)誤的意思是說(shuō),不能先select出同一表中的某些值,再u(mài)pdate這個(gè)表(在同一語(yǔ)句中)。 例如下面這個(gè)sql:
代碼如下:
delete from tbl where id in
(
select max(id) from tbl a where EXISTS
(
select 1 from tbl b where a.tac=b.tac group by tac HAVING count(1)>1
)
group by tac
)
改寫(xiě)成下面就行了:
代碼如下:
delete from tbl where id in
(
select a.id from
(
select max(id) id from tbl a where EXISTS
(
select 1 from tbl b where a.tac=b.tac group by tac HAVING count(1)>1
)
group by tac
) a
)
也就是說(shuō)將select出的結(jié)果再通過(guò)中間表select一遍,這樣就規(guī)避了錯(cuò)誤。注意,這個(gè)問(wèn)題只出現(xiàn)于mysql,mssql和oracle不會(huì)出現(xiàn)此問(wèn)題。
更多信息請(qǐng)查看IT技術(shù)專欄