34,838
社区成员




[code=SQL]create table T (id int)
insert into T select 1
insert into T select 2
insert into T select 3
insert into T select 5
insert into T select 5
insert into T select 7
insert into T select 7
insert into T select 8
insert into T select 9
go
select * from T a where (select count(1) from T where ID=a.ID)>1--显示重复记录
/*
id
-----------
5
5
7
7
(所影响的行数为 4 行)
*/
--删除
go
select distinct * into # from T
truncate table T
insert T select * from #
--查看
select * from T
/*
id
-----------
1
2
3
5
7
8
9
(所影响的行数为 7 行)
*/
[/code]declare @tb table (id int)
insert into @tb select 1
insert into @tb select 2
insert into @tb select 3
insert into @tb select 5
insert into @tb select 5
insert into @tb select 7
insert into @tb select 7
insert into @tb select 8
insert into @tb select 9
select *
from @tb a
where exists (select id from @tb where a.id = id group by id having count(1) > 1)
/*
id
-----------
5
5
7
7
(所影响的行数为 4 行)
*/
select *
from @tb a
where id in (select id from @tb group by id having count(1) > 1)
/*
id
-----------
5
5
7
7
(所影响的行数为 4 行)
*/
declare @tb table (id int,name varchar(10))
insert into @tb select 1,'a'
insert into @tb select 2,'b'
insert into @tb select 3,'c'
insert into @tb select 5,'d'
insert into @tb select 5,'d'
insert into @tb select 7,'e'
insert into @tb select 7,'e'
insert into @tb select 8,'f'
insert into @tb select 9,'g'
select * from @tb group by id,name
declare @tb table (id int)
insert into @tb select 1
insert into @tb select 2
insert into @tb select 3
insert into @tb select 5
insert into @tb select 5
insert into @tb select 7
insert into @tb select 7
insert into @tb select 8
insert into @tb select 9
select distinct id from @tb a