2013-10-03 110 views
0

我嘗試創建mysql事件應該刪除重複的表的行。mysql刪除子查詢不能正常工作的查詢

這是我的查詢

DELETE FROM locations 
WHERE id NOT IN (SELECT id 
       FROM locations 
       GROUP BY acc_id,`date`) 

我得到這個錯誤:

Error Code: 1093 
You can't specify target table 'locations' for update in FROM clause. 

如何更改查詢,使其工作?

+0

重複:http://stackoverflow.com/questions/2630440/how-to-delete-duplicates-on-mysql-table – crafter

回答

3

在MySQL中,您無法從您正在選擇的同一張表中刪除。但是,你可以欺騙的MySQL與另一個子查詢

DELETE FROM locations 
WHERE id NOT IN 
(
    select * from 
    (
     SELECT id 
     FROM locations 
     GROUP BY acc_id, `date` 
) x 
) 
0

儘量提供您的子查詢中的自定義的別名,你不能直接指定更新同桌/刪除

DELETE FROM locations 
WHERE id NOT IN (
SELECT new_table.id FROM (
SELECT id 
FROM locations 
GROUP BY acc_id,`date` 
) new_table  
) 
+1

它的工作表示感謝。 – Lusi