sql 刪除表中的反復記載。本站提示廣大學習愛好者:(sql 刪除表中的反復記載)文章只能為提供參考,不一定能成為您想要的結果。以下是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 >= '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 )
以上就是本文的全部內容,希望本文的內容對大家的學習或許任務能帶來一定的協助,同時也希望多多支持!