我可以在 SQL Server 数据库上使用 LIMIT N,M

发布于 2024-12-15 01:14:29 字数 359 浏览 0 评论 0原文

我有以下 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

离旧人 2024-12-22 01:14:29

要回答您的查询,您可能需要:(取决于您的 MN 值)

WITH cte AS
(
   SELECT [id], [name], ROW_NUMBER() OVER (ORDER BY t2.[iid] ASC) AS rowNumber
   FROM [dbo.test_db_002] t1
   LEFT JOIN [dbo.test_db_003] t2 ON t1.[id] = t2.[itmid]
)
SELECT [id], [name]
FROM cte
WHERE rowNumber BETWEEN 3 AND 5

需要注意的是,两者之间的值是 BETWEEN N AND N + M

此外,这里还有一个链接,其中包含有关 Common Table 的信息表达式这是我使用的 WITH cte 语法。

To answer for your query, you may want: (depending on your values for M and N)

WITH cte AS
(
   SELECT [id], [name], ROW_NUMBER() OVER (ORDER BY t2.[iid] ASC) AS rowNumber
   FROM [dbo.test_db_002] t1
   LEFT JOIN [dbo.test_db_003] t2 ON t1.[id] = t2.[itmid]
)
SELECT [id], [name]
FROM cte
WHERE rowNumber BETWEEN 3 AND 5

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.

毅然前行 2024-12-22 01:14:29

SQL Server 中没有与 LIMIT N,M 直接等效的方法,但您可以执行以下操作:

SELECT * FROM
  (SELECT *, ROW_NUMBER() OVER (ORDER BY name) as row FROM MyTable) a
WHERE row > 5 and row <= 10

请参阅此处了解更多信息:SQL Server 中的“LIMIT”

There's no direct equivalent to LIMIT N,M in SQL Server, but you can do something like this:

SELECT * FROM
  (SELECT *, ROW_NUMBER() OVER (ORDER BY name) as row FROM MyTable) a
WHERE row > 5 and row <= 10

See here for some more info: "LIMIT" in SQL Server

霞映澄塘 2024-12-22 01:14:29

您可以使用 Row_Number()

示例:

select * from 
(
  select cola, colb, row_number() over (order by col1 desc) as row
  from table ) x
where row between value1 and value2

You could use Row_Number()

example:

select * from 
(
  select cola, colb, row_number() over (order by col1 desc) as row
  from table ) x
where row between value1 and value2
等待我真够勒 2024-12-22 01:14:29

sql server 2012 中的偏移量限制:

SELECT email FROM myTable
OFFSET 10 ROWS
FETCH NEXT 10 ROWS ONLY;

//offset - no。跳过的行数

//下一个 - 必需的数量下一行

Limit with offset in sql server 2012:

SELECT email FROM myTable
OFFSET 10 ROWS
FETCH NEXT 10 ROWS ONLY;

//offset - no. of skipped rows

//next - required no. of next rows

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文