文章來自此人博客:
http://hi.baidu.com/zg110/blog,在這里表示感謝!
表stuinfo,有三個(gè)字段recno(自增),stuid,stuname
建該表的Sql語(yǔ)句如下:
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ù)的,就顯示兩條)
--這種方成績(jī)的前提是:需有一個(gè)不重復(fù)的列,本例中的是recno
--如:查找stuid重復(fù)的記錄
select * from stuinfo s1
where recno not in (
select max(recno) from stuinfo s2
where s1.stuid=s2.stuid
)