Created
August 9, 2018 12:43
-
-
Save WaximeA/032753c43863362d9cce18c0460272f8 to your computer and use it in GitHub Desktop.
SQL : delete duplicate rows except one
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
DELETE FROM myTable | |
WHERE (col1, col2, col2, col3) | |
IN ( | |
SELECT * FROM ( | |
SELECT col1, col2, col2, col3 | |
FROM myTable | |
GROUP BY col1, col2, col2, col3 | |
HAVING count(*) > 1 | |
) AS T | |
-- remove this where condition if you want to delete all duplicate rows | |
WHERE id NOT IN ( | |
SELECT * FROM ( | |
SELECT id | |
FROM myTable | |
GROUP BY col1, col2, col2, col3 | |
HAVING count(*) > 1 | |
) AS T | |
) | |
); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment