如何用一条sql取得第10到第20条的记录?

因为id可能不是连续的,所以不能用取得10<id<20的记录的方法。

有两个方法可以实现:
一、搜索前20条记录,指定不包括前10条
语句:
select top 20 * from tbl where id not in (select top 10 id from tbl)

二、搜索记录生成临时表,建立临时表的自增id。通过取得自增id的10<id<20的记录的方法取得所需数据
语句:
select identity(int,1,1) as id,* into #temp from tbl;

select * from #temp where id between 10 and 20

补充:
SELECT TOP 10 * FROM
(SELECT TOP 20 * FROM TableName ORDER BY 1 ASC)
ORDER BY 1 DESC

posted @ 2008-01-09 10:39  RobotTech  阅读(496)  评论(1编辑  收藏  举报