1

我正在使用此查询进行分页

 SELECT * 
 FROM 
    (SELECT ROW_NUMBER() OVER (ORDER BY {0} {1}) AS RowNum, * 
     FROM Cars 
     WHERE IdOwner = {2}) AS Rows 
 WHERE RowNum > {3} AND RowNum < {4}

我想在 2 个表上进行分页查询,而不是像现在这样只在一个表上进行。

SELECT * 
FROM 
    (SELECT ROW_NUMBER() OVER (ORDER BY {0} {1}) AS RowNum, 
            Cars.Id, Cars.Make, Cars.Model, Color.Name 
     FROM Cars 
     INNER JOIN Color ON Cars.ColorId = Color.Id 
     WHERE IdOwner = {2}) AS Rows 
WHERE RowNum > {3} AND RowNum < {4} 

我得到一个Incorrect syntax near the keyword 'WHERE'.

问题解决了。谢谢大家,我是个菜鸟:(

SELECT * 
FROM ( SELECT ROW_NUMBER() OVER ( ORDER BY c.Id  ) AS RowNum, 
       c.Id, c.Make, c.Model, d.ColorName 
       FROM Car AS c 
       INNER JOIN Color AS d 
       ON c.ColorId = d.Id WHERE c.IdOwner=1 ) AS Rows
WHERE RowNum > 0 AND RowNum < 11 
4

2 回答 2

4

这可能是一个括号问题,您必须在您的 IdOwner 前面加上前缀。请注意为您的 {0} 和 {1} 参数添加前缀:

SELECT ROW_NUMBER() OVER ( ORDER BY {0} {1} ) AS RowNum, 
       Cars.Id,Cars.Make, Cars.Model, Color.Name 
FROM (Cars INNER JOIN Color ON Cars.ColorId=Color.Id) 
WHERE Cars.IdOwner={2} 
于 2011-03-09T15:33:09.960 回答
1

c.Make 被多次指定,并且“AS Rows”已被删除并且是必需的。

SELECT * 
FROM ( SELECT ROW_NUMBER() OVER ( ORDER BY c.Id  ) AS RowNum, 
       c.Id, c.Make, c.Model, d.ColorName 
       FROM Car AS c 
       INNER JOIN Color AS d 
       ON c.ColorId = d.Id WHERE c.IdOwner=1 ) AS Rows
WHERE RowNum > 0 AND RowNum < 11 
于 2011-03-09T16:58:49.060 回答