假设我们有一个mytable表,且该表id字段有重复值,需要仅保留重复记录中的一条记录即可
- 通过rowid(根据max或min函数判断当前表某些字段重复但rowid不重复的记录)
--查询
select * from mytable a where a.rowid != (select max(rowid) from mytable b where b.id = a.id);
--删除
delete from mytable a where a.rowid != (select max(rowid) from mytable b where b.id = a.id);
- 通过group by(根据group by和having判断重复字段并通过max函数取rowid)
--查询
select a.id,count(a.id) from mytable a group by a.id having count(a.id) > 1 order by a.id;
--删除
delete from mytable a where rowid in (select max(rowid) from mytable b group by b.id having count(b.id) > 1);