select top 10 * from (select top 40 * from a order by id asc

select top 10 * from (select top 40 * from a order by id asc) top_n order by id desc
以上这句是你写的,取出表A中第31到第40记录(SQLServer2005,以自动增长的ID作为主键,注意:ID可能不是连续的.)
我想问下能不能将这段代码给讲解下.select top 40是查询前40条数据吧.那么括号外的select top 10是查询40条数据里的前10条.那不是和题目的意思不同了嘛.但我放到数据库里是能查到的.请讲解下,
织梦师 1年前 已收到1个回答 举报

1jianqingxin 春芽

共回答了15个问题采纳率:93.3% 举报

这个语句第一部分
(select top 40 * from a order by id asc) top_n
查出前40条数据,表的别名为 top_n
再 order by id desc逆序查询出前10条,
也就是40到31条
所以就能取到结果
若还有疑问请直接HI我.

1年前

2
可能相似的问题
Copyright © 2024 YULUCN.COM - 雨露学习互助 - 16 q. 1.014 s. - webmaster@yulucn.com