SQL Server數(shù)據(jù)庫多種方式查找重復(fù)記錄
SQL Server數(shù)據(jù)庫多種方式查找重復(fù)記錄:
示例:表stuinfo,有三個(gè)字段recno(自增),stuid,stuname
建該表的Sql語句如下:
CREATE TABLE [StuInfo] (
[recno] [int] IDENTITY (1, 1) NOT NULL ,
[stuid] [varchar] (10) COLLATE Chinese_PRC_CI_AS NOT NULL ,
[stuname] [varchar] (10) COLLATE Chinese_PRC_CI_AS NOT NULL
) ON [PRIMARY]
GO
1.查某一列(或多列)的重復(fù)值(只可以查出重復(fù)記錄的值,不能查出整個(gè)記錄的信息)
例如:查找stuid,stuname重復(fù)的記錄
select stuid,stuname from stuinfo
group by stuid,stuname
having(count(*))>1
2.查某一列有重復(fù)值的記錄(此方法查出的是所有重復(fù)的記錄,如果有兩條記錄重復(fù)的,就查出兩條)
例如:查找stuid重復(fù)的記錄
select * from stuinfo
where stuid in (
select stuid from stuinfo
group by stuid
having(count(*))>1
)
3.查某一列有重復(fù)值的記錄(只顯示多余的記錄,也就是說如果有三條記錄重復(fù)的,就顯示兩條)
前提:需有一個(gè)不重復(fù)的列,此示例為recno。
例如:查找stuid重復(fù)的記錄
select * from stuinfo s1
where recno not in (
select max(recno) from stuinfo s2
where s1.stuid=s2.stuid
關(guān)于SQL Server數(shù)據(jù)庫中查詢重復(fù)記錄的方法就為大家介紹到這,這里介紹的方法可能也是不夠全面的,以后如果有了更新的方法,我會(huì)及時(shí)與大家繼續(xù)分享,希望對(duì)大家能有所幫助。
【編輯推薦】