Skip to content

Instantly share code, notes, and snippets.

@Simcamb
Last active October 10, 2015 13:27
Show Gist options
  • Save Simcamb/3696896 to your computer and use it in GitHub Desktop.
Save Simcamb/3696896 to your computer and use it in GitHub Desktop.
Find and delete duplicates in mySQL on multiple columns
--Find Duplicates
SELECT t.ID, t.id_A, t.id_B
FROM (
SELECT id_A, id_B
FROM table_name
GROUP BY id_A, id_B
HAVING count(*) > 1
) x, table_name t
WHERE x.id_A = t.id_A AND x.id_B = t.id_B
ORDER BY t.id_A, t.id_B
--Delete duplicates
DELETE FROM table_name WHERE id IN (
SELECT * FROM (
SELECT t.ID
FROM (
SELECT id_A, id_B
FROM table_name
GROUP BY id_A, id_B
HAVING count(*) > 1) x, table_name t
WHERE x.id_A = t.id_A AND x.id_B = t.id_B
ORDER BY t.id_A, t.id_B
) AS p
)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment