如何限制查询结果(在我的情况下约为60K行)并仅从X行选择Y行?
如果我使用ROW_NUMBER()我不喜欢我的查询,因为它涉及2个选择查询..一个返回行,一个选择我需要的部分
更新
这是我现在使用的查询:
SELECT *
FROM (
SELECT row_number() OVER (ORDER BY E.LastChangeDate DESC) AS row, E.*, U.[DisplayName] AS EntryCreatorDisplayName, U.[Email] AS EntryCreatorEmail
FROM entries e
INNER JOIN
users u
ON e.fk_user= u.id
WHERE e.EntryRank = 2
AND u.Administrator = 1
) as TableWithRows
WHERE (row >= 31 AND row <= 60)
答案 0 :(得分:6)
WITH q AS
(
SELECT TOP (@Y) m.*, ROW_NUMBER() OVER (ORDER BY mycol) AS rn
FROM mytable m
ORDER BY
mycol
)
SELECT *
FROM q
WHERE rn >= @X
在SQL Server 2000
:
SELECT *
FROM (
SELECT TOP (@Y - @X) *
FROM (
SELECT TOP (@X) *
FROM mytable
ORDER BY
mycol
) q
ORDER BY
mycol DESC
) q2
ORDER BY
mycol
答案 1 :(得分:0)
Row_Number()函数可用于此目的。请参阅以下文章以了解用法 http://www.databasejournal.com/features/mssql/article.php/3572301/RowNumber-function-in-SQL-Server-2005.htm
答案 2 :(得分:0)
此刻暂时无法访问SQL,但是这样会有用吗?
SELECT tempid=IDENTITY(int, 1, 1), * FROM tbl WHERE tempid >= @x AND tempid <= @y
答案 3 :(得分:-1)
我不太熟悉MSSQL,但在MySQL中,我会做这样的事情:
LIMIT 50, 10
其中10是要跳过的记录数,50是要采用的数字。