从 MS Access table 返回第 n 行时出现问题
Problem with returning nth row from MS Access table
我正在尝试 select MS Access (Office 365) 中 table 的第 n 行。我见过以下两种解决方案:
他们都没有为我工作。当我根据这些答案编写查询时,查询返回了 table 中的最后 n 行,然后 selected 了其中的第一个结果。例如。如果我要查看 select 第三行,它会 select 倒数第三行。这是我的查询:
SELECT TOP 1 Sense.SenseID
FROM
(
SELECT TOP 3 Sense.SenseID
FROM Sense
ORDER BY Sense.SenseID DESC
)
ORDER BY Sense.SenseID ASC
知道我做错了什么,以及如何生成正确的结果吗?
您需要一个 table 别名以便语法正确。试试这个:
SELECT TOP 1 s.SenseID
FROM (SELECT TOP 3 s.SenseID
FROM Sense as s
ORDER BY s.SenseID DESC
) as s
ORDER BY s.SenseID ASC;
这假定 Sense.SenseID
是唯一的 -- 但这似乎是一个合理的假设。
顺序应该颠倒:
SELECT TOP 1
s.SenseID
FROM
(SELECT TOP 3 s.SenseID
FROM Sense AS
ORDER BY s.SenseID Asc) AS s
ORDER BY
s.SenseID Desc;
我正在尝试 select MS Access (Office 365) 中 table 的第 n 行。我见过以下两种解决方案:
他们都没有为我工作。当我根据这些答案编写查询时,查询返回了 table 中的最后 n 行,然后 selected 了其中的第一个结果。例如。如果我要查看 select 第三行,它会 select 倒数第三行。这是我的查询:
SELECT TOP 1 Sense.SenseID
FROM
(
SELECT TOP 3 Sense.SenseID
FROM Sense
ORDER BY Sense.SenseID DESC
)
ORDER BY Sense.SenseID ASC
知道我做错了什么,以及如何生成正确的结果吗?
您需要一个 table 别名以便语法正确。试试这个:
SELECT TOP 1 s.SenseID
FROM (SELECT TOP 3 s.SenseID
FROM Sense as s
ORDER BY s.SenseID DESC
) as s
ORDER BY s.SenseID ASC;
这假定 Sense.SenseID
是唯一的 -- 但这似乎是一个合理的假设。
顺序应该颠倒:
SELECT TOP 1
s.SenseID
FROM
(SELECT TOP 3 s.SenseID
FROM Sense AS
ORDER BY s.SenseID Asc) AS s
ORDER BY
s.SenseID Desc;