按组选择字段数

发布于 2024-12-06 03:02:21 字数 610 浏览 1 评论 0原文

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

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`

这些似乎不起作用。

有人可以帮忙吗?

霍什

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

陌伤浅笑 2024-12-13 03:02:21

这应该有效...

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

This should work...

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

夏天碎花小短裙 2024-12-13 03:02:21
select sum(score),type from table group by type
select sum(score),type from table group by type
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文