0

我是 PHP 新手,甚至是使用 MS SQL 而非 MySQL 的 PHP 新手。在做了一些研究之后,我想出了一个可以考虑使用分页查询的方法,但显然我在某个地方出错了,因为它会引发错误。我不明白为什么。

继承人的查询:

SELECT TOP 10 * FROM Products WHERE SubCatID = '".$scid."' and ProductID NOT IN ( SELECT TOP 0 * FROM Products ORDER BY ProductID ASC ) ORDER BY ProductID ASC

继承人的错误:

Warning: mssql_query() [function.mssql-query]: message: Only one expression can be specified in the select list when the subquery is not introduced with EXISTS. (severity 16)

我知道这是因为子查询,但是任何人都可以指出我正确的方向吗?

4

2 回答 2

2

您必须在子查询中只选择一列。例子

SELECT TOP 10 * FROM Products WHERE SubCatID = '".$scid."' and ProductID NOT IN ( SELECT TOP 0 ProductID FROM Products ORDER BY ProductID ASC ) ORDER BY ProductID ASC
于 2012-11-29T10:43:05.187 回答
0

一个更好的分页查询是这样的:

SELECT * FROM (
  SELECT Products.*, 
         ROW_NUMBER() OVER (ORDER BY ProductID) AS __RN
    FROM Products WHERE SubCatID = '".$scid."' ORDER BY ProductID
) numberedRows
WHERE __RN BETWEEN 1 and 10

然后,您可以将最后一行的数字替换为所需的行号。通常,您不应该使用 *。实际上,您将在结果集上获得一个额外的列,称为__RN. 如果您指定列名,则不会发生这种情况。

于 2012-11-29T10:55:19.867 回答