SqlServer中怎么删除重复的记录(表中没有id)
其实我在别的网址也查到过删除重复的记录,不知道我是我SqlServer2012版本太低还是啥原因
delete from sc
where (c#,s#) in (select c#,s# from sc group by c#,s# having count(1)>1)and rowid not in (select min(rowid) from sc group by c#,s# having count(1)>1)像这个就执行不通,(真想骂人),还tm这么多,首先这个表中你得有个rowid,我们平时都不会加的,而且where (s#,c#) in(select S#,c# from ...) 报错
所以,我就查查查,写写写,终于可以不用表中id就可以删除了。
先建立一个2个表Student和SC
use test
create table Student(S# varchar(10),Sname nvarchar(10),Sage datetime,Ssex nvarchar(10))
insert into Student values('01' , N'赵雷' , '1990-01-01' , N'男')
insert into Student values('02' , N'钱电' , '1990-12-21' , N'男')
insert into Student values('03' , N'孙风' , '1990-05-20' , N'男')
insert into Student values('04' , N'李云' , '1990-08-06' , N'男')
insert into Student values('05' , N'周梅' , '1991-12-01' , N'女')
insert into Student values('06' , N'吴兰' , '1992-03-01' , N'女')
insert into Student values('07' , N'郑竹' , '1989-07-01' , N'女')
insert into Student values('08' , N'王菊' , '1990-01-20' , N'女')
insert into Student values('08' , N'王菊' , '1990-01-20' , N'女')
create table Course(C# varchar(10),Cname nvarchar(10),T# varchar(10))
insert into Course values('01' , N'语文' , '02')
insert into Course values('02' , N'数学' , '01')
insert into Course values('03' , N'英语' , '03')
create table Teacher(T# varchar(10),Tname nvarchar(10))
insert into Teacher values('01' , N'张三')
insert into Teacher values('02' , N'李四')
insert into Teacher values('03' , N'王五')
create table SC(S# varchar(10),C# varchar(10),score decimal(18,1))
insert into SC values('01' , '01' , 80)
insert into SC values('01' , '02' , 90)
insert into SC values('01' , '03' , 99)
insert into SC values('02' , '01' , 70)
insert into SC values('02' , '02' , 60)
insert into SC values('02' , '03' , 80)
insert into SC values('03' , '01' , 80)
insert into SC values('03' , '02' , 80)
insert into SC values('03' , '02' , 80)
insert into SC values('03' , '02' , 80)
insert into SC values('03' , '03' , 80)
insert into SC values('04' , '01' , 50)
insert into SC values('04' , '02' , 30)
insert into SC values('04' , '03' , 20)
insert into SC values('05' , '01' , 76)
insert into SC values('05' , '02' , 87)
insert into SC values('06' , '01' , 31)
insert into SC values('06' , '03' , 34)
insert into SC values('07' , '02' , 89)
insert into SC values('07' , '03' , 98)
insert into SC values('07' , '03' , 98)
insert into SC values('07' , '03' , 98)
go
1、删除student表中s#重复的记录
1.1重复的只有一个字段时
1.1查出重复的记录来
由于student表中没有id,所以我们先搞个查询语句,并且自己创造个id
select s#,row_number() over(partition by s# order by s#) as id from Student
注释:
row_number() over(partition by s# order by s#)
按s#进行分组排序,row_number()表示s#相同情况下也进行排序,详细用法可以百度,谢谢1.2删除重复的记录
with ab as(
select * from(select s#,row_number() over(partition by s# order by s#) as id from Student ) a
where id>1) delete from ab注释(可不看):
因为where id>1是指将id=1的字段保留,多余的删除
此公式Student是表名,s#是字段名,套用公式,只要修改有颜色的表名和字段名就行,其他可以不用管。
2.1删除SC中s#,c#两个字段均重复的记录(删除多个字段重复的记录)
2.1.1查询多个字段重复的记录,与1.1相似,就不解释了
查询表SC中s#,c#两个字段均重复的记录,也标记为id
select s#,c# ,row_number() over(partition by s#,c# order by s#,c#) as id from sc
2.1.2删除该记录
删除id大于1的记录,由于id在where里面不能直接使用,所以要嵌套一个select * from (... )a where a.id>1
with abc as
(select * from(select s#,c# ,row_number() over(partition by s#,c# order by s#,c#) as id from sc) a
where id>1) delete from abc注释:
s#,c#分别是【字段名1】和【字段名2】,而sc是表名,删除时套用以上公式修改这些有颜色的字就行。
with ... as 可以自行百度看
共同分享,如有更好的方法请留言,谢谢!