22,209
社区成员
发帖
与我相关
我的任务
分享
create table #tb(uname varchar(10), score int, location varchar(10) , remark varchar(10) )
insert #tb select 'mary',30,'sh','hello'
insert #tb select 'mary',40,'sz','thanks'
insert #tb select 'kate',24,'sd','hi'
insert #tb select 'kate',45,'uy','sorry'
insert #tb select 'moss',28,'sh','hello'
insert #tb select 'moss',0,'sz','hi'
go
select * from #tb a where exists(select 1 from #tb where uname=a.uname and score<a.score)
uname score location remark
---------- ----------- ---------- ----------
mary 40 sz thanks
kate 45 uy sorry
moss 28 sh hello
(3 行受影响)
create table tb(uname varchar(10), score int, location varchar(10) , remark varchar(10) )
go
insert tb select 'mary' ,30, 'sh' , 'hello'
insert tb select 'mary', 40, 'sz' , 'thanks'
insert tb select 'kate' ,24, 'sd' , 'hi'
insert tb select 'kate' ,45, 'uy' , 'sorry'
insert tb select 'moss', 28, 'sh' , 'hello'
insert tb select 'moss', 0, 'sz' , 'hi'
go
select * from tb a WHERE score = (SELECT MAX(score) FROM tb WHERE uname = a.uname) ORDER BY uname
go
drop table tb
go
/*
uname score location remark
---------- ----------- ---------- ----------
kate 45 uy sorry
mary 40 sz thanks
moss 28 sh hello*/
create table tb(uname varchar(10), score int, location varchar(10) , remark varchar(10) )
go
insert tb select 'mary' ,30, 'sh' , 'hello'
insert tb select 'mary', 40, 'sz' , 'thanks'
insert tb select 'kate' ,24, 'sd' , 'hi'
insert tb select 'kate' ,45, 'uy' , 'sorry'
insert tb select 'moss', 28, 'sh' , 'hello'
insert tb select 'moss', 0, 'sz' , 'hi'
go
select * from tb a where not exists(select 1 from tb where uname=a.uname and score>a.score)
go
drop table tb
go
/*
uname score location remark
---------- ----------- ---------- ----------
mary 40 sz thanks
kate 45 uy sorry
moss 28 sh hello
(所影响的行数为 3 行)
*/
select * from tb a where not exists(select 1 from tb where uname=a.uname and score>a.score)