SQL 查找重復(fù)記錄(轉(zhuǎn))
文章來自此人博客:http://hi.baidu.com/zg110/blog,在這里表示感謝!
表stuinfo,有三個字段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ù)記錄的值,不能整個記錄的信息) --如:查找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ù)的,就顯示兩條) --這種方成績的前提是:需有一個不重復(fù)的列,本例中的是recno --如:查找stuid重復(fù)的記錄 select * from stuinfo s1 where recno not in ( select max(recno) from stuinfo s2 where s1.stuid=s2.stuid )
|
posted on 2007-06-19 17:13 liujg 閱讀(330) 評論(0) 編輯 收藏 所屬分類: 數(shù)據(jù)庫