sql 删除表中的重复记录

嗯,遇见了表中存在重复的记录的问题,直接写sql删除时最快的,才不要慢慢的复制到excel表中慢慢的人工找呢。哼。

如下sql,找出重复的记录,和重复记录中ID值最小的记录(表中ID为自增长)

select MIN(ID) as id, StructSN ,Date,UserID,StarCount,COUNT(StructSN) as c
from T_Dor_StructStar
where Date >= ''
group by StructSN ,Date,UserID,StarCount
having COUNT(StructSN) >

然后就可以直接删除,基本原理就是,找到重复记录的每一条记录,排除掉重复id最小的记录,删除剩余的重复记录。

delete from T_Dor_StructStar
where ID in (
select s.ID from T_Dor_StructStar s,
(
select MIN(ID) as id, StructSN ,Date,UserID,StarCount,COUNT(StructSN) as c
from T_Dor_StructStar
where Date >= ''
group by StructSN ,Date,UserID,StarCount
having COUNT(StructSN) >
)a
where
a.Date = s.Date
and a.StructSN = s.StructSN
and a.UserID = s.UserID
and a.StarCount = s.StarCount
and a.id != s.ID
)
上一篇:"无法删除数据库,因为该数据库当前正在使用"问题解决


下一篇:JS实现Ajax,Josn数据的序列化和反序列化---例: 省市区联动(包含get,post)