Skip to content

Instantly share code, notes, and snippets.

@fcaldarelli
Last active May 8, 2018 14:30
Show Gist options
  • Save fcaldarelli/a3088773f2ac3b0efcba9c965c04254b to your computer and use it in GitHub Desktop.
Save fcaldarelli/a3088773f2ac3b0efcba9c965c04254b to your computer and use it in GitHub Desktop.
Mysql remove duplicates
### 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