我使用SQL Server查询表中的某些行,我需要在行20-30之间返回元素,这是怎么回事?
这是我的疑问:
select *
from PostsView
where EnumKey = " & CategoryId & "
and IsDeleted = 'False'
order by CreationDate desc
请帮帮我
答案 0 :(得分:1)
一种方法:
select TOP 11 *
from (select TOP 30 *
from PostsView
where EnumKey = " & CategoryId & " And IsDeleted = 'False'
Order By CreationDate Desc
) t
Order By CreateDate ASC;
(您可以使用其他子查询再次颠倒顺序。)
或者,在更新版本的SQL Server中:
OFFSET 19 FETCH NEXT 11 ROWS
或者,使用row_number()
。
答案 1 :(得分:0)
您可以指定行号,然后选择行20-30(假设您在应用where子句后需要行20-30,而不是基于表的完整内容)。
;With CTE as (
select *, row_number over (order by CreationDate Desc) as RN
from PostsView
where EnumKey = " & CategoryId & "
And IsDeleted = 'False')
Select * from CTE where RN between 20 and 30