日期:2014-05-18  浏览次数:20405 次

sql server 查询重复数据
id name age
1 张三 20
2 张三 21
3 张三 28
4 李四 22  
5 王五 29
6 王五 23

id为主键
查询重复的记录


显示结果
1 张三 20
2 张三 21
3 张三 28
5 王五 29
6 王五 23

------解决方案--------------------
SQL code
select * from tb a
 where exists(select 1 from tb where name=a.name and id<>a.id)

------解决方案--------------------
SQL code
create table tb(id int,name varchar(10),age int)
insert into tb select 1,'张三',20
insert into tb select 2,'张三',21
insert into tb select 3,'张三',28
insert into tb select 4,'李四',22
insert into tb select 5,'王五',29
insert into tb select 6,'王五',23
go
select * from tb where name in(select name from tb group by name having count(*)>1)
/*
id          name       age
----------- ---------- -----------
1           张三         20
2           张三         21
3           张三         28
5           王五         29
6           王五         23

(5 行受影响)

*/
go
drop table tb