按组选择字段数

时间:2011-09-22 12:23:00

标签: mysql group-by distinct

我有一张包含下表的表格:

id | score | type
 1 |     1 |  "a"
 2 |     4 |  "b"
 3 |     2 |  "b"
 4 |    53 |  "c"
 5 |     8 |  "a"

我需要根据类型选择得分计数。所以我的结果将是:

totalScore | type
         9 |  "a"
         6 |  "b"
        53 |  "c"

我尝试了DISTINCTGROUP BY似乎都没有效果。我试过的SQL:

SELECT * , COUNT(  `score` ) AS totalScore
FROM  `table`
GROUP BY  `type`

SELECT DISTINCT `type`, COUNT(score) as totalScore FROM `table`

但这些似乎不起作用。

有人可以帮忙吗?

Hosh

2 个答案:

答案 0 :(得分:3)

这应该有用......

SELECT sum(score) AS total FROM table GROUP BY type

答案 1 :(得分:3)

select sum(score),type from table group by type