1

我有这个在大多数情况下都有效的内部连接语句,但是当我添加自定义分页时,连接返回一个空集。任何建议将不胜感激。

这将返回我期望的结果

SELECT [Id]
,[Title]
FROM 
(SELECT [Id]
,[Title]
,ROW_NUMBER() OVER(ORDER BY id) as RowNum from Art) as e
INNER JOIN [ArtCat] p ON e.Id = p.ArtId 
WHERE 
p.CatId = @CategoryNum

这将返回一个空集

SELECT [Id]
,[Title]
FROM 
(SELECT [Id]
,[Title]
,ROW_NUMBER() OVER(ORDER BY id) as RowNum from Art) as e
INNER JOIN [ArtCat] p ON e.Id = p.ArtId 
WHERE 
p.CatId = @CategoryNum
AND  RowNum BETWEEN @startIndex AND (@startIndex + @pageSize) 

提前致谢!

4

2 回答 2

0

山羊CO你真的很有帮助!谢谢。我们最终使用的代码是。

SELECT
       ROW_NUMBER() OVER(ORDER BY id) as e.RowNum  
       ,e.[Id]
      ,e.[Title]

FROM (SELECT  
          [Id]
         ,[Title]
         ,ROW_NUMBER() OVER(ORDER BY id) as RowNum         
  FROM 
     (SELECT
           ROW_NUMBER() OVER(ORDER BY id) as RowNum
           ,[Id]
           ,[Title]

           FROM Art
              INNER JOIN [ArtCat] p ON e.Id = p.ArtId 

       WHERE p.CatId = @CategoryNum
     ) as e
WHERE 
  e.RowNum BETWEEN @startIndex AND (@startIndex + @pageSize) 

预过滤结果(就像你有的那样),但在外部表上创建另一行以真正组织结果。

我的同事也抱怨我的格式太..哈

于 2013-09-26T15:42:45.413 回答
0

格式化有点难读,但语法很好,你为什么不检查你的 rownum 的边界:

SELECT MIN(RowNum),MAX(RowNum)          
FROM (SELECT [Id]
            ,[Title]
            ,ROW_NUMBER() OVER(ORDER BY id) as RowNum 
      FROM Art
      ) as e
INNER JOIN [ArtCat] p ON e.Id = p.ArtId 
WHERE p.CatId = @CategoryNum

奇怪的是你应该在限制后使用 RowNump.CatId = @CategoryNum

猜测这可行:

SELECT [Id]
      ,[Title]
FROM (SELECT  [Id]
             ,[Title]
             ,ROW_NUMBER() OVER(ORDER BY id) as RowNum         
      FROM (SELECT [Id]
                  ,[Title]
            FROM Art
            ) as e
      INNER JOIN [ArtCat] p ON e.Id = p.ArtId 
      WHERE p.CatId = @CategoryNum
     )sub
WHERE RowNum BETWEEN @startIndex AND (@startIndex + @pageSize) 
于 2013-09-25T21:18:00.837 回答