Get another order after limit

…衆ロ難τιáo~ 提交于 2019-12-21 07:28:27

问题


Imagine I've a table 'users' with two fields: 'age' and 'name'. I want to retrieve the top ten older users and then I want this list of ten sorted by name.

Is it possible to do it with MySQL?

I've tried this: (doesn't work)

SELECT * FROM users order by age, name limit 10

回答1:


Use a subselect:

SELECT * FROM
(
    SELECT *
    FROM users
    ORDER BY age DESC
    LIMIT 10
) AS T1
ORDER BY name

The inner select finds the 10 rows you want to return, and the outer select puts them in the correct order.



来源:https://stackoverflow.com/questions/7786570/get-another-order-after-limit

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!