在PHP中我正在进行SQL Server查询,我需要(LIMIT)类型功能。我已经知道TOP通常用在SQL Server而不是LIMIT(这是MySQL语法)。但是,TOP不允许您像LIMIT那样设置'offset'。在我的查询中,我需要“偏移”和“行数”是动态的。示例:
LIMIT $startRow, $maxRow
有任何想法或建议吗?
答案 0 :(得分:1)
Sql Server 2005没有上面的限制语法,直到2012年才添加。您将需要使用OVER子句和公用表表达式。
WITH Members AS
(
SELECT M_NAME, M_POSTS, M_LASTPOSTDATE, M_LASTHEREDATE, M_DATE, M_COUNTRY,
ROW_NUMBER() OVER (ORDER BY M_POSTS DESC) AS RowNumber
FROM dbo.FORUM_MEMBERS
)
SELECT RowNumber, M_NAME, M_POSTS, M_LASTPOSTDATE, M_LASTHEREDATE, M_DATE, M_COUNTRY
FROM Members
WHERE RowNumber BETWEEN 1 AND 20
ORDER BY RowNumber ASC;