lessismore
lessismore

Reputation: 177

the sum of fields values from a column with a condition


I am quite new to mySQL and need help.
I have a database like this (values are example):

name   |region        |population|
----------------------------------
Cuba   |Carribean     |10        |
Ukraine|Eastern Europe|15        |
Belarus|Eastern Europe|9         |
Haiti  |Carribean     |3         | 

I want to find total population of the region (e.g. all population from Eastern Europe) and print as a table the region name and its total population.

But I have no idea how to find a sum of field value from one column but under condition from another column.

How to to do this query?

Upvotes: 1

Views: 78

Answers (2)

chirag satapara
chirag satapara

Reputation: 1937

Try below query:

select name , region , sum(population)
from region_table // table_name
group by  name , region

May be this will help you.

Upvotes: 0

Daniel E.
Daniel E.

Reputation: 2480

SELECT region, SUM(population) AS population
FROM table 
GROUP BY region

Upvotes: 1

Related Questions