SQL 如何按范围选择行?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/14068815/
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 12:51:20  来源:igfitidea点击:

How can I select rows by range?

sqlsqlite

提问by rtheunissen

Possible Duplicate:
Select statement in SQLite recognizing row number

可能的重复:
SQLite 中的 Select 语句识别行号

For example, SELECT * FROM table WHERE [row] BETWEEN x AND y

例如, SELECT * FROM table WHERE [row] BETWEEN x AND y

How can this be done? I've done some reading but haven't found anything specifically correct.

如何才能做到这一点?我已经阅读了一些资料,但没有发现任何特别正确的内容。

Imagine a list where you want results paged by an X amount of results, so for page 10 you would need results from rows 10 * X to 10 * X + X. Rather than display ALL results in one go

想象一个列表,您希望结果按 X 数量分页,因此对于第 10 页,您需要从 10 * X 到 10 * X + X 行的结果。而不是一次性显示所有结果

回答by Nandkumar Tekale

For mysql you have limit, you can fire query as :

对于 mysql 你有限制,你可以触发查询:

SELECT * FROM table limit 100` -- get 1st 100 records
SELECT * FROM table limit 100, 200` -- get 200 records beginning with row 101

For Oracle you can use rownum

对于 Oracle,您可以使用rownum

See mysql select syntax and usage for limithere.

请参阅limit此处的mysql select 语法和用法。

For SQLite, you have limit, offset. I haven't used SQLite but I checked it on SQLite Documentation. Check example for SQLite here.

对于 SQLite,您有limit, offset. 我没有使用过 SQLite,但我在SQLite Documentation上检查过它。在此处检查 SQLite 的示例。

回答by Majid Laissi

You can use rownum:

您可以使用rownum

SELECT * FROM table WHERE rownum > 10 and rownum <= 20

回答by Pez Cuckow

Following your clarificationyou're looking for limit:

根据您的说明,您正在寻找限制:

SELECT * FROM `table` LIMIT 0, 10 

This will display the first 10 results from the database.

这将显示数据库中的前 10 个结果。

SELECT * FROM `table` LIMIT 5, 5 .

Will display 5-9 (5,6,7,8,9)

将显示 5-9 (5,6,7,8,9)

The syntax follows the pattern:

语法遵循以下模式:

SELECT * FROM `table` LIMIT [row to start at], [how many to include] .


The SQL for selecting rows where a columnis between two values is:

用于选择位于两个值之间的行的 SQL是:

SELECT column_name(s)
FROM table_name
WHERE column_name
BETWEEN value1 AND value2

See: http://www.w3schools.com/sql/sql_between.asp

见:http: //www.w3schools.com/sql/sql_between.asp

If you want to go on the row number you can use rownum:

如果你想继续行号,你可以使用rownum:

SELECT column_name(s)
FROM table_name
WHERE rownum 
BETWEEN x AND y

However we need to know which database engine you are using as rownum is different for most.

但是,我们需要知道您使用的是哪个数据库引擎,因为 rownum 与大多数情况不同。

回答by Jayamohan

Using Between condition

使用条件之间

SELECT *
FROM TEST
WHERE COLUMN_NAME BETWEEN x AND y ;

Or using Just operators,

或者使用 Just 运算符,

SELECT *
FROM TEST
WHERE COLUMN_NAME >= x AND COLUMN_NAME   <= y;

回答by Pharaoh

Have you tried your own code?
This should work:

您是否尝试过自己的代码?
这应该有效:

SELECT * FROM people WHERE age BETWEEN x AND y

回答by Rajshri

Use the LIMITclause:

使用LIMIT子句:

/* rows x- y numbers */
SELECT * FROM tbl LIMIT x,y;

refer : http://dev.mysql.com/doc/refman/5.0/en/select.html

参考:http: //dev.mysql.com/doc/refman/5.0/en/select.html

回答by Raghvendra Parashar

Assuming idis the primary key of table :

假设id是 table 的主键:

SELECT * FROM table WHERE id BETWEEN 10 AND 50

For first 20 results

对于前 20 个结果

SELECT * FROM table order by id limit 20;