
实现方式如下:
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的即为有重复的记录。
下面举个例子供参考:
有雇员表(工号,姓名,身份z号码)
其中字段"身份z号码"因未设置唯一索引,存在重复的情况,现要求检索出身份z有重复的员工资料,SQL实现语句如下
select a.* from 雇员 a,(select 身份z号码 from 雇员 group by 身份z号码 having count(*)>1) b
where a.身份z号码=b.身份z号码
查看可用如下方法: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)
说明:查询的结果就是香蕉和葡萄在表中是有重复的,要把香蕉和葡萄的所有记录都查询出来,结果如图:
欢迎分享,转载请注明来源:内存溢出
微信扫一扫
支付宝扫一扫
评论列表(0条)