各位用户为了找寻关于sql 删除表中的重复记录的资料费劲了很多周折。这里教程网为您整理了关于sql 删除表中的重复记录的相关资料,仅供查阅,以下为您介绍关于sql 删除表中的重复记录的详细内容
遇见了表中存在重复的记录的问题,直接写sql删除时最快的,才不要慢慢的复制到excel表中慢慢的人工找呢
如下sql,找出重复的记录,和重复记录中ID值最小的记录(表中ID为自增长)
? 1 2 3 4 5select
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最小的记录,删除剩余的重复记录。
? 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17delete
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
)
以上就是本文的全部内容,希望本文的内容对大家的学习或者工作能带来一定的帮助,同时也希望多多支持!
原文链接:http://www.cnblogs.com/yucaoye/p/6255691.html