select count of field by group
I have a table with the following table:
id | score | type
1 | 1 | "a"
2 | 4 | "b"
3 | 2 | "b"
4 | 53 | "c"
5 | 8 | "a"
I need to select the count of score based on the type. So my results will be:
totalScore | type
9 | "a"
6 | "b"
53 | "c"
I tried both DISTINCT and GROUP BY by neither seemed to work. 开发者_如何学运维The SQLs I tried:
SELECT * , COUNT( `score` ) AS totalScore
FROM `table`
GROUP BY `type`
and
SELECT DISTINCT `type`, COUNT(score) as totalScore FROM `table`
But these don't seem to work.
Can anyone help?
Hosh
This should work...
SELECT sum(score) AS total FROM table GROUP BY type
select sum(score),type from table group by type
加载中,请稍侯......
精彩评论