Skip to main content

Delete duplicates and keep the newest one in MySQL.

DELETE mytable
    FROM mytable
    INNER JOIN (
            SELECT max(id) AS lastId, someColumnWithDupes
            FROM mytable
            GROUP BY someColumnWithDupes
            HAVING count(*) > 1
            ) duplic ON duplic.someColumnWithDupes = mytable.someColumnWithDupes
    WHERE mytable.id < duplic.lastId;