大家知道sql刪除表重復記錄嗎?用SQL語句,刪除掉重復項只保留一條在幾千條記錄裡,存在著些相同的記錄,如何能用SQL語句,刪除掉重復的呢?
如下sql,找出重復的記錄,和重復記錄中ID值最小的記錄(表中ID為自增長)
select MIN(ID) as id, StructSN ,Date,UserID,StarCount,COUNT(StructSN) as cfrom T_Dor_StructStar where Date >= '20160919'group by StructSN ,Date,UserID,StarCounthaving COUNT(StructSN) > 1
然後就可以直接刪除,基本原理就是,找到重復記錄的每一條記錄,排除掉重復id最小的記錄,刪除剩余的重復記錄。
delete from T_Dor_StructStarwhere ID in (select s.ID from T_Dor_StructStar s,(select MIN(ID) as id, StructSN ,Date,UserID,StarCount,COUNT(StructSN) as cfrom T_Dor_StructStar where Date >= '20160919'group by StructSN ,Date,UserID,StarCounthaving COUNT(StructSN) > 1)awherea.Date = s.Dateand a.StructSN = s.StructSNand a.UserID = s.UserIDand a.StarCount = s.StarCountand a.id != s.ID)
相信大家已經了解sql刪除表重復記錄了吧!感謝大家對我們網站的支持!
相關推薦:
mysql優化插入記錄速度的方法