Blog Entry 13:

Recent Posts

28 April 2015 06:30

Blog Entry 13: !! Clones !!

Finding and Removing Duplicates in mysql !

You have a table with duplicate rows – somehow a unique index didn’t get created and a bug has added duplicate records to your table. A pox upon that bug! Here’s an easy way to clean out that table quickly.




Good but not perfect Say you have a tables where the duplicate field is the “telefonnummber”, i.e 0049 4561387974 appears too many times. The query below will group all the duplicates into one row.
SELECT * FROM table WHERE 1 GROUP BY [duplicate column]; like this SELECT * FROM users GROUP BY telefonnummber;
Perfect and Good Create the new table first like this
CREATE new_table LIKE old_table
This will create a new table with the exact same structure, without the data. NOTE: It also doesn’t preserve the Indexes.
And then copy all the data(non-duplicate across) using a INSERT…SELECT statement

INSERT INTO new_table SELECT * FROM old_table GROUP BY “telefonnummber”

Remember “telefonnummber” is the field you want to remove duplicates by or on.
These are not the only two methods for doing this, there are so many, for example(My personal favourite, one liner)

CREATE TABLE new_table SELECT * FROM old_table group by “telefonnummber”

Does all the work, copies all the data, again indexes not preserved
And there you go, you have a new table with no duplicates. Disclaimer: Double check.

Powered by the Unique-Portal

Spotlight

Server Hardware

All about the Server Hardware

3 x Duel Intel Xeon E5-1231 v3, 4x 3.40GHz 8 Cores (24 Cores)
3 x AsRock Intel Sockel
3 x 32 GB(96Gb) Ram
3 x Crucial 256GB(786GB) 2,5 SSD
3 x Crucial 500GB (1500GB) 2,5 SSD
5 x 4 TB (20TB) 3,5 HDD
3 x be quiet

!!! Unlimited Storage can be offered if requested !!!




We don't support Data Analyzers/Sellers