27,579
社区成员
发帖
与我相关
我的任务
分享
-- 可以考虑使用 not exists
create table test(id int,name varchar(10))
go
insert into test(id,name)
values(1,'daolu'),(2,'daolu'),(3,'shuini'),(4,'shuini'),(5,'guanzu')
go
select * from test
go
select * from test
where not exists(select * from test t where name = test.name and test.id >id)
go
drop table test
go
(5 行受影响)
id name
----------- ----------
1 daolu
2 daolu
3 shuini
4 shuini
5 guanzu
(5 行受影响)
id name
----------- ----------
1 daolu
3 shuini
5 guanzu
(3 行受影响)