Skip to content

Instantly share code, notes, and snippets.

@Simcamb
Created September 24, 2012 14:40
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Simcamb/3776272 to your computer and use it in GitHub Desktop.
Save Simcamb/3776272 to your computer and use it in GitHub Desktop.
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;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment