帮忙解决一个SQL问题,排序问题..

来源:百度知道 编辑:UC知道 时间:2024/06/07 14:36:23
表AA:
id title content
1 aa aaaa
2 bb bbbb
3 . .
. . .
. . .
60 n nnnn
我想取值,最后20条的内容(也就是从id41-60),且输出的时候是先从ID41开始输出,要怎么办?
用select top 20 * from AA order by id desc 则是从60-41开始输出..不是我想要的.
select * from AA where id >40 order by id asc;
这句不行,因为前提是我不知道数据总共有多少,我只想取最后20条。数据随时会添加的。

select * from (select top 20 * from AA order by id desc) order by id desc
试试

select * from AA
where id in (select top 20 id from AA order by id desc)
order by id

select * from AA where id >40 order by id asc;
你写一句查询总共有多少条数据,减去20.id>总条数-20就可以了吗

select * from AA where id >40 order by id asc;

简单
select top20 * from AA
where id not in (select top(40)* from AA )