我写了一个查询,从这样的表中检索前5条记录
SELECT Top(5) Activity, GETUTCDATE() as CurrentDate
FROM Activities
WHERE UserID = @uid
ORDER BY ActivityDate DESC
并且在View上我显示前5条记录,我还有一个更多关于点击下面5条记录应该显示的链接,再次点击该链接显示接下来的5条记录等等
我对如何编写此查询感到困惑,即如何获得接下来的5条记录然后接下来的5条等等
答案 0 :(得分:2)
我使用RowNumber函数和CTE在你的例子中实现分页,这就像是。
With UserActivityData as (
SELECT *,
ROW_NUMBER() OVER(ORDER BY ActivityDate DESC) as RowNum,
ROW_NUMBER() OVER(ORDER BY ActivityDate ASC) as InverseRowNum
FROM Activities
WHERE UserID = @uid
)
SELECT * from UserActivityData where RowNum between @StartIndex and @StartIndex + 4