1. 程式人生 > >delete from 使用子查詢的限制

delete from 使用子查詢的限制

在使用 mysql 進行 delete from 操作時,如果子查詢中 from子句 和更新/刪除物件使用同一張表就會出錯。

例:    delete from 表名 

           where f_1 in 

           (select f_1 from t_1 group by f_2);

ERROR 1093 (HY000): You can't specify target table 'test3' for update in FROM clause

撇開效率不談,可以多加一層select 來執行:

改正:    delete from 表名 

               where f_1 in 

                (select t.f_1 from (select * from t_1 group by f_2) as t );