Order by COUNT per value

前端 未结 4 2118
时光说笑
时光说笑 2020-11-27 05:12

I have a table which stores IDs and the city where the store is located.

I want to list all the stores starting with the stores that are in the city where there are

4条回答
  •  旧时难觅i
    2020-11-27 05:30

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

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

提交回复
热议问题