View Single Post
  #1   IP: 153.99.38.51
Old 2015-04-24, 04:06 AM
Randle Randle is offline
初级会员
 
Join Date: 2011-05-07
Posts: 1
Randle 现在声名狼藉
Default Mysql delete duplicate records leaving one

Mysql delete duplicate records leaving one row. Consider that there are many duplicate records in the table and you want to remove it. Now, we can select it by applying GROUP BY but the question is how to delete all the duplicate records (of field some_id below) EXCEPT one that should be there. So, lets say I have following table with duplicate data as shown:
Tablename: test


unique_id | some_id
--------------------
1 | 1
2 | 1
3 | 2
4 | 2
5 | 1


Solution:

DELETE t1 FROM test t1, test t2 WHERE t1.unique_id > t2.unique_id AND t1.some_id = t2.some_id;


Final Result:

unique_id | some_id
--------------------
1 | 1
3 | 2
Reply With Quote