avien
avien

Reputation: 177

counting column in database mysql

how can i count the value of two different column in database?

example:

i have a table hospital which have a column , hosptal_id and sex

now i want a result that count the the sex column by type, (male or female) within a hospital

i want the result something like..

hospital_id ------ F ------ M

1 ----------------- 14----- 45

2 ----------------- 23----- 49

3 ----------------- 29------ 76

, how can i get this result?

is anybody can help me on this?

Upvotes: 0

Views: 110

Answers (2)

sel
sel

Reputation: 4957

SELECT id,SUM(IF(sex='M',1,0)) AS 'male', 
SUM(IF(sex='F',1,0)) AS 'female' 
FROM hospital 
GROUP BY id

Upvotes: 2

swapnesh
swapnesh

Reputation: 26722

Try query like this--

SELECT COUNT(DISTINCT column_name) AS some_alias FROM table_name

Upvotes: 0

Related Questions