前言:解决表中重复数据
- 查看表中重复数据
SELECT * FROM 表 WHERE 字段 IN (SELECT 字段 FROM 表 GROUP BY 字段 HAVING COUNT(字段)>1);
- 查询出要保留的重复数据中最小id值
SELECT MIN(id) ids FROM 表 GROUP BY 字段 HAVING COUNT(字段)>1;
- 这里举例保留最小id值数据,那么需要删除—除最小id值外的其他数据
DELETE FROM 表
WHERE 1=1
AND 字段 IN (SELECT * FROM ( (SELECT 字段 FROM 表 GROUP BY 字段 HAVING COUNT(字段)>1) ) a)
AND id NOT IN (SELECT * FROM ( (SELECT MIN(id) ids FROM 表 GROUP BY 字段 HAVING COUNT(字段)>1) ) b)