id name age
1 张三 20
2 张三 21
3 张三 28
4 李四 22
5 王五 29
6 王五 23id为主键
查询重复的记录
显示结果
1 张三 20
2 张三 21
3 张三 28
5 王五 29
6 王五 23
1 张三 20
2 张三 21
3 张三 28
4 李四 22
5 王五 29
6 王五 23id为主键
查询重复的记录
显示结果
1 张三 20
2 张三 21
3 张三 28
5 王五 29
6 王五 23
where exists(select 1 from tb where name=a.name and id<>a.id)
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