获取sql serverl数据库第10到20条数据

2024-11-18 03:52:47
推荐回答(3个)
回答1:

SQL Server 2008 与以下版本, 可这么写:

SELECT top 10 * FROM
(
select top 20 * from News where news_class_id=109 order by news_id desc

) top_20
order by news_id

SQL Server 2012 可这么写:

select * from News where news_class_id=109 order by news_id desc
OFFSET
10 ROWS
FETCH NEXT
10 ROWS ONLY;

回答2:

select * from(
select *, ROW_NUMBER() OVER(order by news_id desc ) as row from News where news_class_id=109
) a
where row between 10 and 20

这个方式条数可以随便取,比较方便,在sql2005上测试通过

回答3:

2005以上版本适用:
;with cte as(

select *,row_number() over(order by news_id desc)rn from News where news_class_id=109
)select * from cte where rn between 10 and 20

----------------
select top 10 * from News where news_class_id=109
and id not in (select top 10 id from news where news_class_id=109
order by news_id desc)
order by news_id desc