Thursday, April 12, 2012

mysql order by with limit

I have a query where I want to get the 20 most popular locations from a table, and then order them alphabetically. I'm wondering if there's a cleaner or more efficient way to do this?



SELECT city FROM (SELECT city, count(*) AS cnt FROM locations GROUP BY city ORDER BY cnt DESC LIMIT 20) s ORDER BY city;




No comments:

Post a Comment