php 如何在mysql中选择前10条记录
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4164151/
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
How to Select first 10 records in mysql
提问by Rajesh
In my database have 100 records but i want only first 10 records in descending order not for whole database in descending order.
在我的数据库中有 100 条记录,但我只需要按降序排列的前 10 条记录,而不是按降序排列整个数据库。
Ex: Database:Records
例如:数据库:记录
1,2,3,4,5,6,,7,8,9,10,11,12....................100.
First 10 Records:
前 10 条记录:
10,9,8,7,6,5,4,3,2,1
回答by Alexey Romanov
If I understand your question correctly,
如果我正确理解你的问题,
SELECT x FROM (SELECT x FROM table ORDER BY x ASC LIMIT 10) ORDER BY x DESC
The SELECT
in parentheses selects the first 10 records (by ascending x
) and the outer SELECT
sorts them in the order you want.
在SELECT
括号中选择前10条记录(按升序x
)和外SELECT
排序在他们想要的顺序。
回答by Nikki
You can use this query:
您可以使用此查询:
SELECT * FROM (SELECT * FROM table ORDER BY * ASC LIMIT 10)
ORDER BY * DESC ;
回答by Pekka
Use LIMIT
. See the mySQL manual on SELECT
使用LIMIT
. 请参阅有关 SELECT的mySQL 手册
For example:
例如:
SELECT id FROM tablename ORDER BY ID LIMIT 0,10
the turning around of the results like you show is then probably best done in PHP using array_reverse()
, I can't think of a easy mySQL way to do this.
像您展示的结果的转变可能最好在 PHP 中使用 完成array_reverse()
,我想不出一个简单的 mySQL 方法来做到这一点。
回答by noblexenon
Use LIMIT
. See the mySQL manual on SELECT
使用LIMIT
. 请参阅有关 SELECT 的 mySQL 手册
For example:
例如:
SELECT id FROM tablename ORDER BY ID LIMIT 0,10
The turning around of the results like you show is then probably best done in PHP using array_reverse()
, I can't think of a easy MySQL way to do this.
像您展示的结果的转变可能最好在 PHP 中使用 完成array_reverse()
,我想不出一个简单的 MySQL 方法来做到这一点。