How to delete duplicate records in MySQL by retaining those fields with data in the duplicate item b
Posted
by NJTechGuy
on Stack Overflow
See other posts from Stack Overflow
or by NJTechGuy
Published on 2010-04-09T18:05:10Z
Indexed on
2010/04/09
18:23 UTC
Read the original article
Hit count: 424
I have few thousands of records with few 100 fields in a MySQL Table.
Some records are duplicates and are marked as such. Now while I can simply delete the dupes, I want to retain any other possible valuable non-null data which is not present in the original version of the record. Hope I made sense.
For instance :
a b c d e f key dupe
--------------------
1 d c f k l 1 x
2 g h j 1
3 i h u u 2
4 u r t 2 x
From the above sample table, the desired output is :
a b c d e f key dupe
--------------------
2 g c h k j 1
3 i r h u u 2
If you look at it closely, the duplicate is determined by using the key (it is the same for 2 records, so the one that has an 'x' for dupe field is the one to be deleted by retaining some of the fields from the dupe (like c, e values for key 1).
Please let me know if you need more info about this puzzling problem.
Thanks a tonne!
p.s : If it is not possible using MySQL, a PERL/Python script sample would be awesome! Thanks!
© Stack Overflow or respective owner