Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PHP count from Database

Tags:

sql

group-by

I have one php page which must show "Total Score" of "COUNTRY" for example

Let's say we have the following...

  • user ABC with SCORE 20 from SUA
  • user DEF with SCORE 7 from CANADA
  • user GHI with SCORE 10 from SUA

Now what i want is to show total SCORE of SUA for example, which will be 30

SELECT score,country,COUNT(*) FROM users WHERE country GROUP BY score
like image 222
David Claudiu Avatar asked Apr 21 '26 02:04

David Claudiu


1 Answers

You can use the sum function.

select sum(score) as 'total', country
from users
group by country

This will return something like:

+-------+---------+
| total | country |
+-------+---------+
|    30 | SUA     |
|     7 | Canada  |
+-------+---------+

And you can also filter out your query by country with the where clause:

select sum(score) as 'total', country
from users
where country = 'Canada'

Which would give the following:

+-------+---------+
| total | country |
+-------+---------+
|     7 | Canada  |
+-------+---------+
like image 190
Chin Leung Avatar answered Apr 23 '26 17:04

Chin Leung