删除所有重复行,只删除MySQL中的一个行?如何删除MySQL表中的所有重复数据?例如,使用以下数据:SELECT * FROM names;+----+--------+| id | name |+----+--------+| 1 | google || 2 | yahoo || 3 | msn || 4 | google || 5 | google || 6 | yahoo |+----+--------+我会用SELECT DISTINCT name FROM names;如果是SELECT查询。我该怎么做DELETE只删除副本而只保留一条记录?
2 回答
慕妹3242003
TA贡献1824条经验 获得超6个赞
id
DELETE FROM NAMES WHERE id NOT IN (SELECT * FROM (SELECT MIN(n.id) FROM NAMES n GROUP BY n.name) x)
id
DELETE FROM NAMES WHERE id NOT IN (SELECT * FROM (SELECT MAX(n.id) FROM NAMES n GROUP BY n.name) x)
添加回答
举报
0/150
提交
取消