从 MySQL 查询返回第 n 条记录
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2224951/
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
Return the nth record from MySQL query
提问by paulrandall
I am looking to return the 2nd, or 3rd, or 4th record from a MySQL query (based on a query by ID ascending)
我希望从 MySQL 查询中返回第二条、第三条或第四条记录(基于 ID 升序查询)
The problem being, I won't know the ID, only that it is the 3rd row in the query.
问题是,我不知道 ID,只知道它是查询中的第三行。
回答by cmptrgeekken
SELECT * FROM table ORDER BY ID LIMIT n-1,1
SELECT * FROM table ORDER BY ID LIMIT n-1,1
It says return one record starting at record n.
它说从记录 n 开始返回一条记录。
回答by user4408343
The accepted answer is wrong by 1, the offset is zero-indexed:
接受的答案错了 1,偏移量为零索引:
From the doc:
从文档:
With two arguments, the first argument specifies the offset of the first row to return, and the second specifies the maximum number of rows to return. The offset of the initial row is 0 (not 1):
SELECT * FROM tbl LIMIT 5,10; # Retrieve rows 6-15
有两个参数,第一个参数指定要返回的第一行的偏移量,第二个参数指定要返回的最大行数。初始行的偏移量为 0(不是 1):
SELECT * FROM tbl LIMIT 5,10; # 检索第 6-15 行
So the correct query would be
所以正确的查询是
SELECT * FROM table ORDER BY ID LIMIT n-1,1
回答by Asesha George
for example "LIMIT 10, 5", it will skip the number of records indicated by the first number and then show the number of records indicated by the second number. In other words it's "LIMIT skip, show".
例如“LIMIT 10, 5”,它会跳过第一个数字指示的记录数,然后显示第二个数字指示的记录数。换句话说,它是“限制跳过,显示”。
SELECT * FROM tblTesting LIMIT 3, 6
will display from 4th record to 9th record, total records displayed 6
将显示从第 4 条记录到第 9 条记录,总共显示 6 条记录
if you want show descending order use DESC
如果你想显示降序使用 DESC
SELECT * FROM tblTesting ORDER BY column_name DESC LIMIT 3, 6
回答by Mike Cialowicz
Use the limit clause (add 'limit 3, 1' to the end of your query to only select the third row).
使用 limit 子句(在查询末尾添加 'limit 3, 1' 以仅选择第三行)。
Here's some more information: http://php.about.com/od/mysqlcommands/g/Limit_sql.htm
这里有更多信息:http: //php.about.com/od/mysqlcommands/g/Limit_sql.htm
回答by Muhammad Abbas
MYSQL: The offset always start from zero-indexed
MYSQL:偏移量总是从零索引开始
OFFSET value means not start from OFFSET value
Example: records 1, 2, 3, 4, 5.
OFFSET 1 means return 2nd value, as OFFSET 2 return 3rd value and so on
OFFSET 值表示不是从 OFFSET 值开始
示例:记录 1、2、3、4、5。
OFFSET 1 表示返回第二个值,OFFSET 2 返回第三个值,依此类推
SELECT table_column FROM Table GROUP BY table_column DESC LIMIT 1 OFFSET 1;
OR
或者
SELECT table_column FROM Table GROUP BY table_column DESC LIMIT 3 OFFSET 1;
It will return 3 records from 2nd record
它将从第二条记录返回 3 条记录
回答by David Culbreth
If you are using PHP to process your records, then you might use the expressions from the PHP manual:
如果您使用 PHP 来处理您的记录,那么您可以使用 PHP 手册中的表达式:
<?php
/* Open a connection */
$link = mysqli_connect("localhost", "my_user", "my_password", "world");
/* check connection */
if (!$link) {
printf("Connect failed: %s\n", mysqli_connect_error());
exit();
}
$query = "SELECT Name, CountryCode FROM City ORDER BY Name";
if ($result = mysqli_query($link, $query)) {
/* seek to row no. 400 */
mysqli_data_seek($result, 399);
/* fetch row */
$row = mysqli_fetch_row($result);
printf ("City: %s Countrycode: %s\n", $row[0], $row[1]);
/* free result set*/
mysqli_free_result($result);
}
/* close connection */
mysqli_close($link);
?>
Read more in the PHP manual: http://php.net/manual/en/mysqli-result.data-seek.php
在 PHP 手册中阅读更多内容:http: //php.net/manual/en/mysqli-result.data-seek.php