在ORDER BY子句中指定非聚合列,或者使用子查询进行排序。
例如,以下查询会出现错误:
SELECT category, COUNT(*) AS count
FROM mytable
GROUP BY category
ORDER BY category, count DESC
可以改写为:
SELECT category, COUNT(*) AS count
FROM mytable
GROUP BY category
ORDER BY category ASC, count DESC
或者使用子查询进行排序:
SELECT category, count
FROM (
SELECT category, COUNT(*) AS count
FROM mytable
GROUP BY category
)
ORDER BY category ASC, count DESC