按组选择字段数
我有一个包含下表的表格:
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"
我尝试了 DISTINCT
和 GROUP BY
,但似乎都不起作用。我尝试过的 SQL:
SELECT * , COUNT( `score` ) AS totalScore
FROM `table`
GROUP BY `type`
但是
SELECT DISTINCT `type`, COUNT(score) as totalScore FROM `table`
这些似乎不起作用。
有人可以帮忙吗?
霍什
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
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
这应该有效...
SELECT sum(score) AS Total FROM table GROUP BY type
This should work...
SELECT sum(score) AS total FROM table GROUP BY type