如何查找数据库中的重复数据?
实现方式如下:
Dim cnn As Object, rs As Object, SQL$, i&, s$
Set cnn = CreateObject("ADODB.Connection")
cnn.Open "Provider=Microsoft.Jet.Oledb.4.0;data Source=" & ThisWorkbook.Path & "\排课数据.mdb"
SQL = "Select 星期&节&班级,count(星期&节&班级) from 排课 where 星期 is not null group by 星期&节&班级 having count(星期&节&班级)>1"
Set rs = CreateObject("ADODB.Recordset")
rs.Open SQL, cnn, 1, 3
If rs.RecordCount Then
For i = 1 To rs.RecordCount
s = s & vbCrLf & "星期" & rs.Fields(0) & "班,重复次数:" & rs.Fields(1)
rs.MoveNext
Next
MsgBox "有" & rs.RecordCount & "条记录重复:" & s
Else
MsgBox "没有发现重复记录"
End If
rs.Close
cnn.Close
Set rs = Nothing
Set cnn = Nothing
End Sub
1。删除全部重复记录(慎用)
Delete 表 Where 重复字段 In (Select 重复字段 From 表 Group By 重复字段 Having Count(*)>1)
2。保留一条(这个应该是大多数人所需要的 ^_^)
Delete HZT Where ID Not In (Select Max(ID) From HZT Group By Title)
注:此处保留ID最大一条记录
3、查找表中多余的重复记录(多个字段)
select * from vitae a
where (a.peopleId,a.seq) in (select peopleId,seq from vitae group by peopleId,seq having count(*) > 1)
4、删除表中多余的重复记录(多个字段),只留有rowid最小的记录
delete from vitae a
where (a.peopleId,a.seq) in (select peopleId,seq from vitae group by peopleId,seq having count(*) > 1)
and rowid not in (select min(rowid) from vitae group by peopleId,seq having count(*)>1)
(数据库执行效率高)
select
*
from
dbo.MediafileInfo
as
a
where
(VideoDownUrl
IN
(SELECT
VideoDownUrl
FROM
MediafileInfo
AS
B
WHERE
A.ProgramID
<>
B.ProgramID))
(数据库执行效率低)
SELECT
*
FROM
MediafileInfo
AS
A
WHERE
(SELECT
COUNT(*)
FROM
MediafileInfo
WHERE
VideoDownUrl=A.VideoDownUrl)>1