返回范围的 SQL Select 查询

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/5525823/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-01 10:00:19  来源:igfitidea点击:

SQL Select query that returns a range

sqlsql-server

提问by Spidy

Possible Duplicate:
Row Offset in MS SQL Server

可能的重复:
MS SQL Server 中的行偏移

I want to select a range from x1 to x2. Just like you can select the top results:

我想选择一个从 x1 到 x2 的范围。就像您可以选择最佳结果一样:

SELECT TOP X * FROM TABLE

SELECT TOP 5 * FROM tUsers

But I would like to select middle results. So if I want results 10-20 is there a way to query that?

但我想选择中间结果。所以如果我想要结果 10-20 有没有办法查询?

SELECT 10-20 * FROM TABLE?

回答by Sandro Munda

With SQL Server :

使用 SQL 服务器:

Row Offset in SQL Server

SQL Server 中的行偏移

With MySQL :

使用 MySQL:

SELECT * FROM `your_table` LIMIT 10, 20

With Oracle :

使用甲骨文:

SELECT * FROM `your_table` WHERE rownum >= 10 and rownum < 20;

With PostgreSQL :

使用 PostgreSQL :

SELECT * FROM `your_table` LIMIT 20 OFFSET 10

`your_table` must be replaced by your real table name

`your_table` 必须替换为您的真实表名

回答by pcofre

In SQL Server 2005 or above you can use a CTEand the ROW_NUMBERfunction:

在 SQL Server 2005 或更高版本中,您可以使用 aCTEROW_NUMBER函数:

WITH TblCte as
(
SELECT  *
        ,ROW_NUMBER() OVER (ORDER BY OrderCol) RowNumber
FROM    Table
)
SELECT  *
FROM    TblCte
WHERE   RowNumber between 10 and 20

In SQL Server 2000 or below, it was quite difficult and inefficient: http://social.msdn.microsoft.com/Forums/en-IE/transactsql/thread/e92d9b03-42ad-4ab9-9211-54215e7b9352

在 SQL Server 2000 或更低版本中,它非常困难且效率低下:http: //social.msdn.microsoft.com/Forums/en-IE/transactsql/thread/e92d9b03-42ad-4ab9-9211-54215e7b9352

回答by Toby Allen

In mysql this is

在mysql中这是

SELECT * FROM table LIMIT 10,20

回答by pgruetter

What DB are you using? If you're on Oracle, try

你用的是什么数据库?如果您使用 Oracle,请尝试

where rownum >= 10 and rownum < 20;