Last active
May 8, 2018 14:30
-
-
Save fcaldarelli/a3088773f2ac3b0efcba9c965c04254b to your computer and use it in GitHub Desktop.
Mysql remove duplicates
This file contains hidden or 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
### SHORT WAY | |
DELETE n1 FROM names n1, names n2 WHERE n1.id > n2.id AND n1.name = n2.name | |
### FAST WAY | |
-- Create temporary table | |
CREATE TABLE temp_table LIKE table1; | |
-- Add constraint | |
ALTER TABLE temp_table ADD UNIQUE(category, image_set_id); | |
-- Copy data | |
INSERT IGNORE INTO temp_table SELECT * FROM table1; | |
-- Rename and drop | |
RENAME TABLE table1 TO old_table1, temp_table TO table1; | |
DROP TABLE old_table1; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment