需求:删除表“m”中字段“d”多余的重复记录,保留id最小的一条。
实现:DELETE FROM m WHERE id NOT IN(
SELECT b.minid FROM (SELECT MIN(id) AS minid FROM m GROUP BY d) b
);
解释:SELECT MIN(id) AS minid FROM m GROUP BY d 查出了需要保留的记录的最小id的集合;
DELETE FROM m WHERE id NOT IN(需要保留的记录的最小id的集合)就可以删除额外的记录了。