首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 数据库 > SQL Server >

sql server 查询重复数据解决思路

2012-01-29 
sql server 查询重复数据id name age1张三 202张三 213张三 284李四 225王五 296王五 23id为主键查询重复

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,'张三',20insert into tb select 2,'张三',21insert into tb select 3,'张三',28insert into tb select 4,'李四',22insert into tb select 5,'王五',29insert into tb select 6,'王五',23goselect * from tb where name in(select name from tb group by name having count(*)>1)/*id          name       age----------- ---------- -----------1           张三         202           张三         213           张三         285           王五         296           王五         23(5 行受影响)*/godrop table tb 

热点排行
Bad Request.