我正在使用T-SQL这样的分页
SELECT @PageSize = 4,
@Page = 1;
SELECT T.* FROM (((SELECT RANK() OVER(PARTITION BY 'T' ORDER BY ID) AS
RowIndex, * FROM VideoGallery) AS T
INNER JOIN Video_Category ON T.ID = Video_Category.VideoID)
INNER JOIN VCategories ON VCategories.Name = N'catname' AND
Video_Category.CategoryID = VCategories.ID)
WHERE T.RowIndex BETWEEN ((@Page - 1) * @PageSize + 1) AND (@Page *
@PageSize);
结果就像:
ROWINDEX NAME
1 backup
2 email
3 fax
4 GSM
5 backup
6 email
13 fax
18 GSM
问题:在第2页中仅显示2个结果 我想在第2页上显示ROWINDEX 13和18
答案 0 :(得分:2)
尝试这种方式
按******订购
OFFSET @ItemsPerPage *(@CurrentPage-1)行
仅次于@ItemsPerPage ROWS
DECLARE @Page int = 1;
DECLARE @PageSize int = 4;
SELECT T.* FROM (((SELECT RANK() OVER(PARTITION BY 'T' ORDER BY ID) AS
RowIndex, * FROM VideoGallery) AS T
INNER JOIN Video_Category ON T.ID = Video_Category.VideoID)
INNER JOIN VCategories ON VCategories.Name = N'catname' AND
Video_Category.CategoryID = VCategories.ID)
ORDER BY T.ID
OFFSET @PageSize * (@Page - 1) ROWS
FETCH NEXT @PageSize ROWS ONLY
答案 1 :(得分:1)
该查询失败,因为您的join
过滤出了一些行号,因此它们不是连续的,并且之间可能会有间隔。
我用适当的缩进重写了您的查询,并删除了不必要的查询嵌套。试试看:
SELECT @PageSize = 4, @Page = 1;
SELECT *
FROM (
SELECT vg.*,
RANK() OVER(/*PARTITION BY 'T' */ORDER BY vc.VideoID) AS RowIndex
FROM VideoGallery AS vg
INNER JOIN Video_Category vc ON vg.ID = vc.VideoID
INNER JOIN VCategories vc2 ON vc2.[Name] = N'catname' AND vc.CategoryID = vc2.ID
) a
WHERE RowIndex BETWEEN ((@Page - 1) * @PageSize + 1) AND (@Page * @PageSize);
此外,如果您确实愿意,我在RANK() OVER(/*PARTITION BY 'T' */ORDER BY ID)
处注释了一部分代码,请取消注释。
答案 2 :(得分:0)
@MorteZa SenFi;看看这个模拟。我认为问题可能出在您对RowIndex的定义上(您应该使用ROW_NUMBER而不是RANK。请检查RowIndex列实际上是连续的还是连续的。
请参见下面的一个简单示例:
DECLARE @PageSize INT = 3
DECLARE @Page INT = 2;
DECLARE @tblVideoGallery TABLE (Id INT, Name VARCHAR(20))
INSERT INTO @tblVideoGallery
SELECT 1,'backup' UNION ALL
SELECT 2, 'email' UNION ALL
SELECT 3, 'fax' UNION ALL
SELECT 4, 'GSM' UNION ALL
SELECT 5, 'backup' UNION ALL
SELECT 6, 'email' UNION ALL
SELECT 7, 'fax' UNION ALL
SELECT 8, 'GSM'
SELECT T.*
FROM (
SELECT ROW_NUMBER() OVER(ORDER BY ID) AS RowIndex, *
FROM @tblVideoGallery
) T
--INNER JOIN Video_Category ON T.ID = Video_Category.VideoID)
--INNER JOIN VCategories ON VCategories.Name = N'catname' AND
--Video_Category.CategoryID = VCategories.ID)
WHERE T.RowIndex BETWEEN ((@Page - 1) * @PageSize + 1) AND (@Page *
@PageSize);