假设有一张表names,内容如下:
SELECT * FROM names;
+----+--------+
| id | name |
+----+--------+
| 1 | google |
| 2 | yahoo |
| 3 | msn |
| 4 | google |
| 5 | google |
| 6 | yahoo |
+----+--------+
如何删除有重复的name对应的行呢?
方法如下:
方法一:删除重复行,保留id最小的行:
DELETE FROM NAMES WHERE id NOT IN (SELECT *
FROM (SELECT MIN(n.id)
FROM NAMES nGROUP BY n.name) x)
方法二:删除重复行,保留id最大的行:
DELETE FROM NAMES WHERE id NOT IN (SELECT *
FROM (SELECT MAX(n.id)
FROM NAMES nGROUP BY n.name) x)