SQL 获取当前第几条记录
表结构
ID 人 分数
1 A01 10
2 A02 20
3 A03 30
4 A04 20
5 A05 40
这个表有10w+记录
要求用6条语句,分别取出最新的6条
SQL? 取最新记录
[解决办法]
select 人, 分数 from
(select 人, 分数, row_number() over(order by ID desc) 'rn'
from
(select top 6 人, 分数, ID
from [表名] order by ID desc) t
) y
where rn=1
select 人, 分数 from
(select 人, 分数, row_number() over(order by ID desc) 'rn'
from
(select top 6 人, 分数, ID
from [表名] order by ID desc) t
) y
where rn=2
select 人, 分数 from
(select 人, 分数, row_number() over(order by ID desc) 'rn'
from
(select top 6 人, 分数, ID
from [表名] order by ID desc) t
) y
where rn=3
select 人, 分数 from
(select 人, 分数, row_number() over(order by ID desc) 'rn'
from
(select top 6 人, 分数, ID
from [表名] order by ID desc) t
) y
where rn=4
select 人, 分数 from
(select 人, 分数, row_number() over(order by ID desc) 'rn'
from
(select top 6 人, 分数, ID
from [表名] order by ID desc) t
) y
where rn=5
select 人, 分数 from
(select 人, 分数, row_number() over(order by ID desc) 'rn'
from
(select top 6 人, 分数, ID
from [表名] order by ID desc) t
) y
where rn=6