Remove duplicate records in mysql

Issue

I have a table called leads with duplicate records

Leads:
*account_id
*campaign_id

I want to remove all the duplicate account_id where campaign_id equal to "51"

For example, if account_id = 1991 appears two times in the table then remove the one with campaign_id = "51" and keep the other one.

Solution

You could use a delete join:

DELETE t1
FROM yourTable t1
INNER JOIN yourTable t2
    ON t2.account_id = t1.account_id AND
       t2.campaign_id <> 51
WHERE
    t1.campaign_id = 51;

Answered By – Tim Biegeleisen

This Answer collected from stackoverflow, is licensed under cc by-sa 2.5 , cc by-sa 3.0 and cc by-sa 4.0

Leave a Reply

(*) Required, Your email will not be published