Delete Duplicate email addresses from Table in MYSQL

前端 未结 5 1286
深忆病人
深忆病人 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: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).

提交回复
热议问题