
实现方式如下:
Dim cnn As Object, rs As Object, SQL$, i&, s$Set cnn = CreateObject("ADODB.Connection")
cnn.Open "Provider=Microsoft.Jet.Oledb.4.0data 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表GroupBy重复字段HavingCount(*)>1)
2。保留一条(这个应该是大多数人所需要的^_^)
DeleteHZTWhereIDNotIn(SelectMax(ID)FromHZTGroupByTitle)
注:此处保留ID最大一条记录
3、查找表中多余的重复记录(多个字段)
select*fromvitaea
where(a.peopleId,a.seq)in(selectpeopleId,seqfromvitaegroupbypeopleId,seqhavingcount(*)>1)
4、删除表中多余的重复记录(多个字段),只留有rowid最小的记录
deletefromvitaea
where(a.peopleId,a.seq)in(selectpeopleId,seqfromvitaegroupbypeopleId,seqhavingcount(*)>1)
androwidnotin(selectmin(rowid)fromvitaegroupbypeopleId,seqhavingcount(*)>1)
查看可用如下方法:1、创建测试表,插入数据:
1
2
3
4
5
6
7
8
9
10
11
12
13
create table product
(id int,
name varchar(10),
totol int)
insert into product values (1,'香蕉',100)
insert into product values (2,'橘子',67)
insert into product values (3,'葡萄',89)
insert into product values (4,'苹果',235)
insert into product values (5,'香蕉',77)
insert into product values (6,'芒果',34)
insert into product values (7,'葡萄',78)
insert into product values (8,'梨',24)
表中数据如:
2、如果查询name列有重复的数据,可执行sql语句:
1
select * from product where name in (select name from product group by name having COUNT(*)>1)
说明:查询的结果就是香蕉和葡萄在表中是有重复的,要把香蕉和葡萄的所有记录都查询出来,结果如图:
两种方法,一种是查询重复的数据,只查询重复记录,不管其余信息,如ID什么的:select uid, time from ztest GROUP BY uid, time having count(*)>1
查出结果是
uid
time
1
1
还有一种是查询你指定信息,可以查询出ID信息:
select distinct a.id, a.uid, a.time from ztest a join ztest b on a.id != b.id where a.uid = b.uid and a.time = b.time
查询结果是:
id
uid
time
1
1
1
3
1
1
欢迎分享,转载请注明来源:内存溢出
微信扫一扫
支付宝扫一扫
评论列表(0条)