开发者

select count of field by group

开发者 https://www.devze.com 2023-04-06 12:39 出处:网络
I have a table with the following table: id | score | type 1 |1 |\"a\" 2 |4 |\"b\" 3 |2 |\"b\" 4 |53 |\"c\"

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
0

精彩评论

暂无评论...
验证码 换一张
取 消