Delete Duplicate email addresses from Table in MYSQL

前端 未结 5 1264
深忆病人
深忆病人 2020-12-18 09:20

I have a table with columns for ID, firstname, lastname, address, email and so on.

Is there any way

相关标签:
5条回答
  • 2020-12-18 09:26
    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.

    0 讨论(0)
  • 2020-12-18 09:31

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

    0 讨论(0)
  • 2020-12-18 09:40
    DELETE n1 FROM customers n1, customers n2 WHERE n1.ID > n2.ID AND n1.email = n2.email
    
    0 讨论(0)
  • 2020-12-18 09:42

    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'
    
    0 讨论(0)
  • 2020-12-18 09:48
    • 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
    0 讨论(0)
提交回复
热议问题