Get another order after the limit

Imagine that I have a user table with two fields: age and name. I want to get the top ten old users, and then I want this list of ten to be sorted by name.

Is it possible to do this with MySQL?

I tried this: (does not work)

SELECT * FROM users order by age, name limit 10 
+12
source share
1 answer

Use the subquery:

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

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

+27
source

All Articles