2012-02-01 38 views
1

我想從我的數據庫中刪除重複的數據。我發現a nice example on here of how to do this on an oracle database使用MySQL刪除表中的重複數據

從答案(只選擇重複行)的作品在MySQL,但刪除查詢(見下文)不會在底部查詢...

"DELETE FROM studios as a 
WHERE a.id > 
     ANY (SELECT b.id 
       FROM studios as b 
      WHERE a.name = b.name 
       AND a.email = b.email 
      )" 

我得到的錯誤是:

You have an error in your SQL syntax; check the manual that corresponds to your 
MySQL server version for the right syntax to use near 'a 
WHERE a.id > 
     ANY (SELECT b.id 
       FROM studios as b 
      ' at line 1 

所以,我看了一下delete syntaxany syntax的使用權限,但找不到任何錯誤的查詢...任何想法?

+0

MySQL不喜歡這裏的'AS'operator。但我不知道如何解決這個問題,而不會出現鋸齒。 – 2012-02-01 09:10:41

+0

我對「ANY」語法不太確定,但是您是否嘗試過使用「IN」語句。例如WHERE a.id IN(SELECT b.id ... – Robert 2012-02-01 09:15:36

回答

2

嘗試此查詢 -

DELETE t1 FROM studios t1 
    JOIN (SELECT MIN(id) id, name, email FROM studios GROUP BY name, email) t2 
    ON t1.id <> t2.id AND t1.name = t2.name AND t1.email = t2.email; 
+0

謝謝!這正是我所需要的... ...仍然好奇爲什麼原始查詢不起作用?我試過它在所有情況下都有或沒有'as',因爲在查看Juergen的答案後可以使用...... – sanukcm 2012-02-01 10:23:35

0

AS運算符不能在MySQL中使用DELETE語句。

試試這個(未驗證):

DELETE FROM a using studios a 
WHERE a.id > 
ANY 
(
    SELECT b.id 
    FROM studios as b 
    WHERE a.name = b.name 
    AND a.email = b.email 
) 
+0

感謝您的回答Juergen!我試過這個查詢,並得到錯誤'您無法指定目標表'a'用於FROM子句中的更新... ... – sanukcm 2012-02-01 10:01:23