Order by COUNT per value Order by COUNT per value mysql mysql

Order by COUNT per value


SELECT count(City), CityFROM tableGROUP BY CityORDER BY count(City);

OR

SELECT count(City) as count, CityFROM tableGROUP BY CityORDER BY count;

Ahh, sorry, I was misinterpreting your question. I believe Peter Langs answer was the correct one.


This one calculates the count in a separate query, joins it and orders by that count (SQL-Fiddle):

SELECT c.id, c.cityFROM cities cJOIN ( SELECT city, COUNT(*) AS cnt       FROM cities       GROUP BY city     ) c2 ON ( c2.city = c.city )ORDER BY c2.cnt DESC;


This solution is not a very optimal one so if your table is very large it will take some time to execute but it does what you are asking.

 select c.city, c.id,       (select count(*) as cnt from city c2        where c2.city = c.city) as order_col from city c order by order_col desc

That is, for each city that you come across you are counting the number of times that that city occurs in the database.

Disclaimer: This gives what you are asking for but I would not recommend it for production environments where the number of rows will grow too large.