嗯,遇見了表中存在重覆的記錄的問題,直接寫sql刪除時最快的,才不要慢慢的複製到excel表中慢慢的人工找呢。哼。 如下sql,找出重覆的記錄,和重覆記錄中ID值最小的記錄(表中ID為自增長) 然後就可以直接刪除,基本原理就是,找到重覆記錄的每一條記錄,排除掉重覆id最小的記錄,刪除剩餘的重覆記錄。 ...
嗯,遇見了表中存在重覆的記錄的問題,直接寫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 )