我的表中有重复项,只有一个不同的字段,必须清理它们,看起来像这样:
id1 | some_name | some_details
id2 | some_name | some_details
id3 | some_name | some_details
id4 | another_name | another_details
id5 | another_name | another_details
id6 | another_name | another_details
...等等
有人可以提供有关删除重复项并保留以下内容的适当SQL脚本的帮助吗?
id1 | some_name | some_details
id4 | another_name | another_details
提前谢谢!
答案 0 :(得分:3)
要select
,只需执行一个GROUP BY
,然后使用min()
来获取每个组的第一个ID:
select min(id), col2, col3
from tablename
group by col2, col3
要delete
行,只需执行GROUP BY
,使用min()
来获取每个组的第一个ID:
delete from tablename t1
where exists (select 1 from tablename t2
where t2.c2 = t1.c2 and t2.c3 = t1.c3
and t2.idcol < t1.idcol)
即如果存在另一列具有相同col2和col3的行,但该行的ID值较低,则删除该行。