1. 程式人生 > >如何解決You can’t specify target table for update in FROM clause錯誤

如何解決You can’t specify target table for update in FROM clause錯誤

mysql中You can’t specify target table for update in FROM clause錯誤的意思是說,不能先select出同一表中的某些值,再update這個表(在同一語句中)。 例如下面這個sql:

delete from target_info_day where id in (select min(id) as id from target_info_day where server_id = 3 group by int_time having count(target_number) > 1)

解決辦法: 將select出的結果再通過中間表select一遍,這樣就規避了錯誤。注意,這個問題只出現於mysql,mssql和oracle不會出現此問題。 將上面的sql修改為:

delete from target_info_day where id in ( select v2.id from ( select min(v1.id) as id from target_info_day v1 where v1.server_id = 3 group by v1.int_time having count(v1.target_number) > 1) v2)