简体   繁体   English

如何在MySQL中按降序对前10个条目进行排序?

[英]How do I sort the top 10 entries in descending order in mySQL?

I have a table holding entries which can be sorted by rank. 我有一个表,其中包含可以按等级排序的条目。 I want to get the top 10 entries (which is simple using SELECT * FROM table ORDER BY rank DESC ), but then I want those entries in descending order, so the one with the lowest rank ends up at the top. 我想获得前10个条目(使用SELECT * FROM table ORDER BY rank DESC可以很简单),但是然后我希望这些条目按降序排列,因此排名最低的条目将排在顶部。 How would I do this? 我该怎么做?

(SELECT * FROM table ORDER BY rank DESC LIMIT 10) ORDER BY rank ASC;

这是您要找的东西吗?

You should be able to do: 您应该能够:

SELECT    * 
FROM      (SELECT * FROM `table` ORDER BY rank DESC LIMIT 10) dt
ORDER BY  dt.rank ASC;

I guess you have a table like this: 我猜你有一个这样的表:

CREATE TABLE `table` (id int, rank int);
INSERT INTO `table` VALUES (1, 20), (2, 19), (3, 18), (4, 17), (5, 16), (6, 15),
                           (7, 14), (8, 13), (9, 12), (10, 11), (11, 10), 
                           (12, 9), (13, 8), (14, 7), (15, 6), (16, 5), (17, 4), 
                           (18, 3), (19, 2), (20, 1);

You would get a result like this: 您将得到如下结果:

+------+------+
| id   | rank |
+------+------+
|   10 |   11 |
|    9 |   12 |
|    8 |   13 |
|    7 |   14 |
|    6 |   15 |
|    5 |   16 |
|    4 |   17 |
|    3 |   18 |
|    2 |   19 |
|    1 |   20 |
+------+------+
10 rows in set (0.02 sec)

UPDATE: 更新:

@onik's solution returns the same result. @onik的解决方案返回相同的结果。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM