开发者

SQL to pull data sorted by column then sort it another way

开发者 https://www.devze.com 2023-01-24 06:27 出处:网络
I\'m pulling the top 15 locations from the database by sorting based on the usedby column, a running total of how many reco开发者_如何学Pythonrds use that location. The problem is, this returns them s

I'm pulling the top 15 locations from the database by sorting based on the usedby column, a running total of how many reco开发者_如何学Pythonrds use that location. The problem is, this returns them sorted by usedby when I'd really like to sort them alphabetically by the name column. I'm thinking this might require some sort of subquery?

SELECT * FROM `location` ORDER BY `usedby` DESC LIMIT 0, 15

Summary: Need to return the above SQL result sorted by the name column.


As you suspected, this can be done with a subquery:

SELECT sq.* FROM (
    SELECT loc.* FROM `location` as loc ORDER BY loc.usedby DESC LIMIT 0, 15
) as sq ORDER BY sq.name
0

精彩评论

暂无评论...
验证码 换一张
取 消