MySQL:计算两个字段

时间:2011-09-05 16:27:55

标签: mysql count

我有一个看起来像这样的表:

Pizza  ----|| Topping 
====================
Cheese--||    Olives
====================
Ham-----     || Tuna
====================
Cheese-- || Olives
====================
Cheese-- || Tuna
====================

我想获得每种类型的比萨饼的数量,因此在上表中将有一个火腿披萨和2个奶酪披萨(因为奶酪+橄榄重复,应该只计算一次)。基本上,我需要计算每两个相等元素的每一行并按比萨分组。

该查询的结果应为:

Pizza  ----|| Count
====================
Cheese--||    2
====================
Ham-----     || 1
====================

谢谢!

2 个答案:

答案 0 :(得分:2)

select Pizza, count( distinct Topping) as cnt
from your_pizza_table
group by Pizza
order by cnt desc;

答案 1 :(得分:0)

试试这个

SELECT pizza, count(distinct pizza,topping) FROM test GROUP BY pizza;