(转)SQL 查找重复记录
生活随笔
收集整理的這篇文章主要介紹了
(转)SQL 查找重复记录
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
表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.--查某一列(或多列)的重復值(只能查出重復記錄的值,不能整個記錄的信息)
--如:查找stuid,stuname重復的記錄
select stuid,stuname from stuinfo
group by stuid,stuname
having(count(*))>1
2.--查某一列有重復值的記錄(這種方法查出的是所有重復的記錄,也就是說如果有兩條記錄重復的,就查出兩條)
--如:查找stuid重復的記錄
select * from stuinfo
where stuid in (
select stuid from stuinfo
group by stuid
having(count(*))>1
)
3.--查某一列有重復值的記錄(只顯示多余的記錄,也就是說如果有三條記錄重復的,就顯示兩條)
--這種方成績的前提是:需有一個不重復的列,本例中的是recno
--如:查找stuid重復的記錄
select * from stuinfo s1
where recno not in (
select max(recno) from stuinfo s2
where s1.stuid=s2.stuid
)
下面這個是查出所有重復記錄的SQL語句:
方法1:??
? SQL>?? Select?? *?? FROM?? table_name?? A?? WHERE?? ROWID?? >?? (??
??????????? SELECT?? min(rowid)?? FROM?? table_name?? B??
??????????? WHERE?? A.key_values?? =?? B.key_values);??
???
? 方法2:??
??????? SQL>?? select?? *?? from?? table_name?? t1??
????????????????? where???? exists?? (select?? 'x'?? from?? table_name?? t2??
????????????????????????????????????????????????? where?? t2.key_value1?? =?? t1.key_value1??
????????????????????????????????????????????????? and?? t2.key_value2?? =?? t1.key_value2??
??????????????????????????????????????????????????? and?? t2.rowid???????????? >?? t1.rowid);
建該表的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.--查某一列(或多列)的重復值(只能查出重復記錄的值,不能整個記錄的信息)
--如:查找stuid,stuname重復的記錄
select stuid,stuname from stuinfo
group by stuid,stuname
having(count(*))>1
2.--查某一列有重復值的記錄(這種方法查出的是所有重復的記錄,也就是說如果有兩條記錄重復的,就查出兩條)
--如:查找stuid重復的記錄
select * from stuinfo
where stuid in (
select stuid from stuinfo
group by stuid
having(count(*))>1
)
3.--查某一列有重復值的記錄(只顯示多余的記錄,也就是說如果有三條記錄重復的,就顯示兩條)
--這種方成績的前提是:需有一個不重復的列,本例中的是recno
--如:查找stuid重復的記錄
select * from stuinfo s1
where recno not in (
select max(recno) from stuinfo s2
where s1.stuid=s2.stuid
)
下面這個是查出所有重復記錄的SQL語句:
方法1:??
? SQL>?? Select?? *?? FROM?? table_name?? A?? WHERE?? ROWID?? >?? (??
??????????? SELECT?? min(rowid)?? FROM?? table_name?? B??
??????????? WHERE?? A.key_values?? =?? B.key_values);??
???
? 方法2:??
??????? SQL>?? select?? *?? from?? table_name?? t1??
????????????????? where???? exists?? (select?? 'x'?? from?? table_name?? t2??
????????????????????????????????????????????????? where?? t2.key_value1?? =?? t1.key_value1??
????????????????????????????????????????????????? and?? t2.key_value2?? =?? t1.key_value2??
??????????????????????????????????????????????????? and?? t2.rowid???????????? >?? t1.rowid);
總結
以上是生活随笔為你收集整理的(转)SQL 查找重复记录的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: jQuery中操作元素节点appendT
- 下一篇: linux cmake编译源码,linu