我有一张桌子:id, Group, Status, Subject
。
我需要其中group = 2 AND status = 2的行,但我想跳过前8个结果(按id
排序)。这是我尝试过的,但它不会跳过前8行:
SELECT TOP 3 [id], LEFT ([subject],30) AS Title
FROM [Katavot]
WHERE (([Status] = @Status) AND ([Group] = @Group))
ORDER BY [id] DESC
答案 0 :(得分:1)
;WITH Katavod_Modified AS
(
SELECT id, LEFT ([subject],30) As Subject, [Group], [Status], ROW_NUMBER() OVER (ORDER BY id) RN
FROM [Katavot]
)
SELECT TOP 3 [id], LEFT ([subject],30) AS Title FROM [Katavod_Modified] WHERE
(([Status] = @Status) AND ([Group] = @Group) AND (RN > 8)) ORDER BY [id] DESC
看起来与this one非常相似。
答案 1 :(得分:1)
WITH MyResult AS
{
SELECT ROW_NUMBER() OVER(ORDER BY Id DESC) AS RowNumber, [id], LEFT ([subject],30) AS Title
FROM [Katavot]
WHERE (([Status] = @Status) AND ([Group] = @Group))
ORDER BY [id] DESC
}
SELECT [id], Title
FROM MyResult
WHERE RowNumber BETWEEN 9 AND 12
答案 2 :(得分:-1)
MySQL的:
LIMIT x, y
MSSQL:
select top y *
from <table>
where <unique id> not in (select top x <unique id> from <table> order by <unique id>)
from <table> order by <unique id>