如何在MYSQL中编写此GROUP BY查询?

时间:2010-04-13 22:32:49

标签: mysql database

假设我有一个名为“水果”的专栏 我想选择所有顶级水果,按水果排序(和+计数组)。

Fruits:
orange
orange
apple
banana
apple
apple

在这种情况下,select语句将返回:

apple, 3
orange, 2
banana, 1

4 个答案:

答案 0 :(得分:4)

未测试:

SELECT 
   fruit_name, 
   COUNT(fruit_id)
FROM
   fruit
GROUP BY
   fruit_name
ORDER BY
   COUNT(fruit_id) DESC

答案 1 :(得分:4)

select fruits, count(fruits)
from table
group by fruits
order by count(fruits) desc

答案 2 :(得分:2)

SELECT fruitname, COUNT(*) AS ttl
FROM fruits 
GROUP BY fruitname 
ORDER BY ttl DESC

答案 3 :(得分:1)

经过测试:

select fruits, count(fruits)
from fruit
group by fruits
order by 2 desc