PHP来自数据库

Dav*_*diu 2 sql group-by

我有一个php页面,例如必须显示"COUNTRY"的"总分"

假设我们有以下......

  • 来自SUA的SCORE 20的用户ABC
  • 用户DEF与加拿大的SCORE 7
  • 来自SUA的SCORE 10的用户GHI

现在我想要的是显示SUA的总分数,例如30分

SELECT score,country,COUNT(*) FROM users WHERE country GROUP BY score
Run Code Online (Sandbox Code Playgroud)

Chi*_*ung 7

您可以使用sum函数.

select sum(score) as 'total', country
from users
group by country
Run Code Online (Sandbox Code Playgroud)

这将返回如下内容:

+-------+---------+
| total | country |
+-------+---------+
|    30 | SUA     |
|     7 | Canada  |
+-------+---------+
Run Code Online (Sandbox Code Playgroud)

您还可以使用where子句按国家/地区过滤掉您的查询:

select sum(score) as 'total', country
from users
where country = 'Canada'
Run Code Online (Sandbox Code Playgroud)

这将给出以下内容:

+-------+---------+
| total | country |
+-------+---------+
|     7 | Canada  |
+-------+---------+
Run Code Online (Sandbox Code Playgroud)