我有以下存储过程,它从SQL Server数据库中为所选照片选择注释。我正在使用OFFSET
和FETCH
来加载帖子(10)的数量,然后有一个按钮,可以使用AJAX将更多帖子添加到DIV。
CREATE PROCEDURE [dbo].[SD_PhotoCmt]
@IDphoto int,
@IDuserInput nvarchar(150),
@offset int,
@fetch int = 10
AS
SET NOCOUNT ON;
SELECT
photoCmt.IDphotoCmt,
photoCmt.photoCmt,
photoCmt.IDuser,
photoCmt.photoCmtDate, //the date and time when the comment was added
photoCmt.IDphoto,
FROM
photoCmt
WHERE
photoCmt.IDphoto = @IDphoto
ORDER BY
photoCmt.photoCmtDate DESC
OFFSET @offset ROWS
FETCH NEXT @fetch ROWS ONLY
假设所选照片有12条评论,我希望评论在页面上显示如下(首次加载时):
所需订单
[Button PREPEND More]
Comment3
Comment4
Comment5
Comment6
Comment7
Comment8
Comment9
Comment10
Comment11
Comment12
不幸的是,存储过程对请求的反馈如下:
不受欢迎的订单
[Button PREPEND More]
Comment12
Comment11
Comment10
Comment9
Comment8
Comment7
Comment6
Comment5
Comment4
Comment3
如何获得我想要的订单?我尝试了很多组合,但我做不到。我无法将TOP
与OFFSET
和FETCH
一起使用。
答案 0 :(得分:2)
首先进行OFFSET / FETCH,然后命令RESULT,如:
select * from
(SELECT
photoCmt.IDphotoCmt,
photoCmt.photoCmt,
photoCmt.IDuser,
photoCmt.photoCmtDate, //the date and time when the comment was added
photoCmt.IDphoto,
FROM photoCmt
WHERE photoCmt.IDphoto = @IDphoto
ORDER BY photoCmt.photoCmtDate DESC
OFFSET @offset ROWS FETCH NEXT @fetch ROWS ONLY)
order by photoCmtDate ASC
答案 1 :(得分:1)
试试这个,
基本上,使用降序将页面转换为子查询,然后在外部查询中重新排序页面。
我会告诫我的建议,使用不同的显示顺序来分页可能会让用户感到非常困惑。
CREATE PROCEDURE [dbo].[SD_PhotoCmt]
@IDphoto int,
@IDuserInput nvarchar(150),
@offset int,
@fetch int = 10
AS
SET NOCOUNT ON;
SELECT
[O].[IDphotoCmt],
[O].[photoCmt],
[O].[IDuser],
[O].[photoCmtDate],
[O].[IDphoto]
FROM
(SELECT
[C].[IDphotoCmt],
[C].[photoCmt],
[C].[IDuser],
[C].[photoCmtDate],
[C].[IDphoto]
FROM
[photoCmt] [C]
WHERE
[C].[IDphoto] = @IDphoto
ORDER BY
[C].[photoCmtDate] DESC
OFFSET @offset ROWS
FETCH NEXT @fetch ROWS ONLY) [O]
ORDER BY
[O].[photoCmt] ASC;
答案 2 :(得分:1)
使用pagination
执行row_number()
并使用Order by
注释列获取所需的订单。试试这个。
SELECT *
FROM (SELECT Row_number()
OVER(
ORDER BY photoCmt.photoCmtDate DESC) rn,
photoCmt.IDphotoCmt,
photoCmt.photoCmt,
photoCmt.IDuser,
photoCmt.photoCmtDate,--the date and time WHEN the comment was added
photoCmt.IDphoto,
FROM photoCmt
WHERE photoCmt.IDphoto = @IDphoto) a
WHERE rn BETWEEN @offset AND @offset + @fetch
ORDER BY CONVERT(INT, Substring(photoCmt, Charindex('[0-9]', photoCmt), Len(photoCmt))) ASC
答案 3 :(得分:0)
使用任何表变量并将查询中的数据加载到表变量中,然后选择该表变量作为您的订单