MySQL 如何在mytable中选择前5个最大值
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/5646748/
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 top 5 max values in mytable
提问by santhosh
Please help me with Query in Mysql.. i am Having table contains lot of rows .now i want retrive the 5 rows from that table.
请帮助我在 Mysql 中查询 .. 我的表包含很多行。现在我想从该表中检索 5 行。
my requirement is top maximum 5 values in that table "column name is amount" i want select from that table.outof N records i need top max 5 records from table
我的要求是该表中最多 5 个值“列名是数量”我想从该表中选择。outof N 条记录我需要表中最多 5 条记录
Thanking you,
感谢您,
回答by Alnitak
Just order the rows by (descending) amount and take the top 5:
只需按(降序)数量对行进行排序并取前 5 个:
SELECT amount FROM mytable ORDER BY amount DESC LIMIT 5
Note that this will result in a full table scan unless you have an index on the amount
column. This could affect performance if the number of rows in the table is very large (i.e. many thousands).
请注意,除非在amount
列上有索引,否则这将导致全表扫描。如果表中的行数非常大(即数千),这可能会影响性能。
回答by Teetrinker
SELECT * FROM table ORDER BY amount DESC LIMIT 5;
SELECT * FROM table ORDER BY amount DESC LIMIT 5;