网络营销电子商务研究中心

网络营销电子商务研究中心 (https://www.0058.net/index.php)
-   MySQL (https://www.0058.net/forumdisplay.php?f=76)
-   -   Mysql delete duplicate records leaving one (https://www.0058.net/showthread.php?t=5112)

Randle 2015-04-24 04:06 AM

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


All times are GMT +8. The time now is 10:38 PM.

Powered by vBulletin Version 3.8.7
Copyright ©2000 - 2026, Jelsoft Enterprises Ltd.