我可以在 SQL Server 数据库上使用 LIMIT N,M
我有以下 SQL 语句:
SELECT [id], [name]
FROM [dbo.test_db_002] t1
LEFT JOIN [dbo.test_db_003] t2 ON t1.[id] = t2.[itmid]
ORDER BY t2.[iid] ASC;
这看起来很简单,但我无法弄清楚。我需要向其中添加 LIMIT N,M
才能从第 N 个项目中检索 M 个项目,但我不断收到“limit”一词周围的错误。我尝试将 LIMIT
子句放在上面 sql 语句中的任何位置,但没有效果。
附言。我正在为 VS2010 附带的 SQL Server 编写。
I have the following SQL statement:
SELECT [id], [name]
FROM [dbo.test_db_002] t1
LEFT JOIN [dbo.test_db_003] t2 ON t1.[id] = t2.[itmid]
ORDER BY t2.[iid] ASC;
This seems very simple, but I can't figure it out. I need to add LIMIT N,M
to it to retrieve M items from the N'th one, but I keep getting errors around 'limit' word. I tried putting that LIMIT
clause everywhere I could inside the sql statement above with no avail.
PS. I'm writing for SQL Server that comes with VS2010.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
要回答您的查询,您可能需要:(取决于您的
M
和N
值)需要注意的是,两者之间的值是
BETWEEN N AND N + M
此外,这里还有一个链接,其中包含有关 Common Table 的信息表达式这是我使用的
WITH cte
语法。To answer for your query, you may want: (depending on your values for
M
andN
)Something to watch out for, the values in the between are
BETWEEN N AND N + M
Also, here's a link with information about Common Table Expressions which is the
WITH cte
syntax I used.SQL Server 中没有与
LIMIT N,M
直接等效的方法,但您可以执行以下操作:请参阅此处了解更多信息:SQL Server 中的“LIMIT”
There's no direct equivalent to
LIMIT N,M
in SQL Server, but you can do something like this:See here for some more info: "LIMIT" in SQL Server
您可以使用 Row_Number()
示例:
You could use Row_Number()
example:
sql server 2012 中的偏移量限制:
//offset - no。跳过的行数
//下一个 - 必需的数量下一行
Limit with offset in sql server 2012:
//offset - no. of skipped rows
//next - required no. of next rows