- 首先根据需要的条件(groub by后的字段为我们去重条件字段)查出重复数据:
SELECT * FROM house_item a GROUP BY a.label,a.parent_code,a.`code`,a.unit,a.floor,a.house_number HAVING count( * ) > 1)
- 因为限定了count(*)>1条件,所以我们如果把查出来的删除,定然是会默认留一条的,直到返回影响条数为0即去重完毕,单次执行完整sql如下:
DELETE FROM house_item
WHERE id IN(
SELECT a.id FROM
(SELECT * FROM house_item a GROUP BY a.label,a.parent_code,a.`code`,a.unit,a.floor,a.house_number HAVING count( * ) > 1) a)
- 递归这个操作直到 Affected rows为0即完成去重,实现方式自定,业务代码或者其他方式自选。