Delete Duplicate email addresses from Table in MYSQL

微笑、不失礼 提交于 2019-11-29 08:00:45

I don't know if this will work in MYSQL (I haven't used it)... but you should be able to do something like the following snippets.

I'd suggest you run them in order to get a feel for if the right data is being selected. If it does work, then you probably want to create a constraint on the column.

Get all of the duplicate e-mail addresses:

SELECT 
    EMAILADDRESS, COUNT(1)
FROM
    TABLE
GROUP BY EMAILADDRESS
HAVING COUNT(1) > 1

Then determine the ID from that gives:

SELECT
    ID
FROM 
    TABLE
WHERE 
    EMAILADDRESS IN (
        SELECT 
            EMAILADDRESS
        FROM
            TABLE
        GROUP BY EMAILADDRESS
        HAVING COUNT(1) > 1
    )

Then finally, delete the rows, based on the above and other constraints:

DELETE 
FROM 
    TABLE
WHERE
    ID IN (
        SELECT
            ID
        FROM 
            TABLE
        WHERE 
            EMAILADDRESS IN (
                SELECT 
                    EMAILADDRESS
                FROM
                    TABLE
                GROUP BY EMAILADDRESS
                HAVING COUNT(1) > 1
            )
    )  
    AND FIRSTNAME = 'Instant'
Alok Nath
DELETE n1 FROM customers n1, customers n2 WHERE n1.ID > n2.ID AND n1.email = n2.email
DELETE FROM table WHERE id NOT IN (SELECT MIN(id) FROM table GROUP BY email)

This keeps the lowest, first inserted id's for every email.

While MiPnamic's answer is essentially correct, it doesn't solve the problem of which record you keep and which you throw away (and how you sort out related records). The short answer is that this cannot be done programmatically.

Given a query like this:

SELECT email, MAX(ID), MAX(firstname), MAX(lastname), MAX(address)
FROM customers

makes it even worse - since you are potentially selecting a mixture of fields from the duplicate rows. You'd need to do something like:

SELECT csr2.*
FROM customers csr2
WHERE ID IN (
   SELECT MAX(id)
   FROM customers csr
   GROUP BY email
);

To get a unique set of existing rows. Of course you still need to sort out all the lreated records (hint - that's the IDs ni customers table not returned by the query above).

  • Duplicate the table structure
  • Put a Unique Key on the email of the new table (just for safe)
  • Do a INSERT on the new table SELECTING data from the older one GROUPING by the email address
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!