Mysql 从表中删除伪重复项
我有一个重复的表,其中只有一个字段不同,必须清理它们,看起来像:Mysql 从表中删除伪重复项,mysql,sql,Mysql,Sql,我有一个重复的表,其中只有一个字段不同,必须清理它们,看起来像: 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
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
提前谢谢你 要选择,只需进行分组,使用min获取每个组的第一个id:
select min(id), col2, col3
from tablename
group by col2, col3
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)
要删除不需要的行,只需执行分组方式,使用min获取每个组的第一个id:
select min(id), col2, col3
from tablename
group by col2, col3
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值较低,则删除该行。按col2、col3分组完成后,不要忘记向表中添加适当的唯一约束,这样就不必再次执行此操作。不要只是拖地,修补漏洞。@Damien_不相信的人,问题是我们使用Redis的cron job作为-is@a_horse_with_no_name我们使用MySQL感谢您的快速响应!基本上我需要删除重复项,在这种情况下如何使用您的方法?