例如,我有一个 php 页面必须显示“国家/地区”的“总分”
假设我们有以下...
- 来自 SUA 的 SCORE 20 的用户 ABC
- 来自加拿大的 SCORE 7 用户 DEF
- 来自 SUA 的 SCORE 10 的用户 GHI
现在我想要的是显示 SUA 的总分,例如 30
SELECT score,country,COUNT(*) FROM users WHERE country GROUP BY score
最佳答案
您可以使用 sum功能。
select sum(score) as 'total', country
from users
group by country
这将返回如下内容:
+-------+---------+
| total | country |
+-------+---------+
| 30 | SUA |
| 7 | Canada |
+-------+---------+
您还可以使用 where 子句按国家过滤查询:
select sum(score) as 'total', country
from users
where country = 'Canada'
这将给出以下内容:
+-------+---------+
| total | country |
+-------+---------+
| 7 | Canada |
+-------+---------+
关于sql - 来自数据库的 PHP 计数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54713211/