Mysql – Merge and then Delete duplicate entries

MySQL

I have mySQL database with some duplicate entries. They have the same field – phone. But they also had fields which differs. At example I have two entries with same phone, but first entry has rating filed = default_value and second entry has rating field = 5.
So I must merge this entries and only then delete duplicates…

More common example:

    entry1.phone==123
entry1.phone==etry2.phone
    entry1.rating!=entry2.phone
    entry1.rating==default_value(0)
    entry2.rating==5
    merge
    entry1.phone==123
    entry1.rating==5
    entry2 is deleted

Best Answer

I don't think you can do this in SQL efficiently. One slow way to do it is something like:

CREATE TEMPORARY TABLE tmp_table (...);
INSERT INTO tmp_table SELECT phone, max(rating) FROM table GROUP BY phone;
TRUNCATE table;
INSERT INTO table SELECT * FROM tmp_table;

A better way would be a stored procedure or an external script. Select all rows from the table ordered by phone and do the grouping/merging/deleting manually (iterate over the results, compare to the phone value from the previous row, if it's different you have a new group, etc.). Writing stored procedures in MySQL is painful though, so I'm not going to write the code for you. :)

Related Topic