遇見了表中存在重復的記錄的問題,直接寫sql刪除時最快的,才不要慢慢的復制到excel表中慢慢的人工找呢
如下sql,找出重復的記錄,和重復記錄中ID值最小的記錄(表中ID為自增長)
select MIN(ID) as id, StructSN ,Date,UserID,StarCount,COUNT(StructSN) as c from T_Dor_StructStar where Date >= '20160919' group by StructSN ,Date,UserID,StarCount having COUNT(StructSN) > 1
然後就可以直接刪除,基本原理就是,找到重復記錄的每一條記錄,排除掉重復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 >= '20160919' group by StructSN ,Date,UserID,StarCount having COUNT(StructSN) > 1 )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 )
以上就是本文的全部內容,希望本文的內容對大家的學習或者工作能帶來一定的幫助,同時也希望多多支持!