代碼
1 尋找重複列:
2
3 select * from ysgg where number in(select number from ysgg group by number having count(number)>1);
4
5
6
7 重複資料刪除列:
8
9 delete from ysgg where number in (select min(number) from ysgg group by number having count(number)>1);
10
代碼
表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
)