62,197
社区成员
发帖
与我相关
我的任务
分享
declare @t table([ID] int identity(1,1),[Name] varchar(10),class int)
insert @t([Name],class) select 'Billy',1
union all select 'Henry',1
union all select 'Lily',2
union all select 'Kevin',1
union all select 'Jerry',2
union all select 'Jimmy',2
union all select 'Terry',3
select * from @t
select * from @t t
where id in(select top 2 id from @t where t.class=class order by id desc)
order by id desc
/*
所影响的行数为 7 行)
ID Name class
----------- ---------- -----------
1 Billy 1
2 Henry 1
3 Lily 2
4 Kevin 1
5 Jerry 2
6 Jimmy 2
7 Terry 3
(所影响的行数为 7 行)
ID Name class
----------- ---------- -----------
7 Terry 3
6 Jimmy 2
5 Jerry 2
4 Kevin 1
2 Henry 1
(所影响的行数为 5 行)
*/
select a.*
from test a
where id
in(
select top 2 id
from test
where class=a.class)
order by id desc