Removing Duplicate Data from a Table using MySQL

白昼怎懂夜的黑 提交于 2020-01-05 07:05:25

问题


I am trying to remove duplicate data from my database. I found a nice example on here of how to do this on an oracle database.

The bottom query from that answer (only selecting the duplicate rows) works in MySQL, but the delete query (see below) does not...

"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
            )"

The error I get is:

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

So, I had a look for the right delete syntax and any syntax to use, but couldn't find anything wrong with my query... Any ideas?


回答1:


Try this query -

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;



回答2:


The AS operator doesn't work with DELETE statements in MySQL.

Try this (unverified):

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
)


来源:https://stackoverflow.com/questions/9093151/removing-duplicate-data-from-a-table-using-mysql

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!