简体   繁体   English

如何将SQL结果从小索引到大索引排序?

[英]How Can I Sort SQL Results From Smallest Index To Largest Index?

I have a database that is sorted by ID.我有一个按 ID 排序的数据库。 I want to echo out the 3 rows with the biggest id number but have them echoed out as我想回显具有最大 id 号的 3 行,但让它们回显为

id = 5
id = 6
id = 7

as opposed to how相对于如何

SELECT * FROM `userinfo`ORDER BY `id` ASC LIMIT 3;

will echo out会回声

id = 7
id = 6
id = 5

in PHP在 PHP 中

Use a derived table for limiting the result and sort again in the outer query.使用派生表来限制结果并在外部查询中再次排序。

SELECT *
       FROM (SELECT *
                    FROM `userinfo`
                    ORDER BY `id` DESC
                    LIMIT 3) x
       ORDER BY `id` ASC;

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

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