Skip to content

Instantly share code, notes, and snippets.

@jdaily
Last active August 29, 2015 14:00
Show Gist options
  • Save jdaily/11386095 to your computer and use it in GitHub Desktop.
Save jdaily/11386095 to your computer and use it in GitHub Desktop.
mySQL dedup
/*********************************************************************************************
Remove duplicate entries / rows a mySQL database table
Credit To:
http://www.justin-cook.com/wp/2006/12/12/remove-duplicate-entries-rows-a-mysql-database-table/
**********************************************************************************************/
/**************************************************************************
Step 1: Move the non duplicates (unique tuples) into a temporary table
***************************************************************************/
CREATE TABLE new_table LIKE old_table;
INSERT new_table SELECT * FROM old_table WHERE 1 GROUP BY [column to remove duplicates by];
/**************************************************************************
Step 2: delete delete the old table
We no longer need the table with all the duplicate entries, so drop it!
***************************************************************************/
DROP TABLE old_table;
/**************************************************************************
Step 3: rename the new_table to the name of the old_table
***************************************************************************/
RENAME TABLE new_table TO old_table;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment