0

我有一个查询,我想从表中获取 20 个最受欢迎的位置,然后按字母顺序排列它们。我想知道是否有更清洁或更有效的方法来做到这一点?

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

1 回答 1

3

稍微干净一点:

SELECT city FROM (
  SELECT city FROM locations 
  GROUP BY city
  ORDER BY count(*) DESC
  LIMIT 20
) s ORDER BY city

count(*)如果您不打算使用它,则不需要检索它。

于 2012-04-12T02:08:31.013 回答