-- 查询出所有重复的身份证号 (365条记录) select c.hzidCard from DAS_householdcode c GROUP BY c.hzidCard HAVING count(c.hzidCard) > 1 ('1','2','3')
-- 然后根据重复的身份证号码 查询出所有重复的记录 select * from DAS_householdcode s where s.hzidCard in ('1','2','3')
-- 所有重复的记录总共 972
-- 607+365=972
-- 查询出要留下那些记录 (留下重复记录中插入时间最晚的记录) select aa.cid from (select c.id cid, max(c.createtime) ctime from DAS_householdcode c GROUP BY c.hzidCard HAVING count(c.hzidCard) > 1) aa
-- 查询出要删除的记录(在查处所有重复记录的基础上,然后not in 要留下的记录) select * from DAS_householdcode s where s.hzidCard in ('1','2','3') and s.id not in (select aa.cid from (select c.id cid, max(c.createtime) ctime from DAS_householdcode c GROUP BY c.hzidCard HAVING count(c.hzidCard) > 1) aa)