mysql去除重复数据

悟已往之不谏,知来者之可追。实迷途其未远,觉今是而昨非。

保留ID最小的

1
DELETE FROM NAMES WHERE id NOT IN( SELECT * FROM (SELECT MIN(n.id)FROM NAMES n GROUPBY n.name) x)

保留ID最大的

1
DELETE FROM NAMES WHERE id NOT IN( SELECT * FROM (SELECT MAX(n.id)FROM NAMES n GROUPBY n.name) x)