faqts : Computers : Programming : Languages : PHP

+ Search
Add Entry AlertManage Folder Edit Entry Add page to http://del.icio.us/
Did You Find This Entry Useful?

2 of 3 people (67%) answered Yes
Recently 2 of 3 people (67%) answered Yes

Entry

I have a table of users. Can I count how many males and how many females without multiple queries?

Feb 29th, 2008 09:58
dman, Cinley Rodick, Ga V, John Marc, Bruce Hartley, http://www.ttnr.org


This is not PHP!
select sum(iif(sex='M',1,0)) as males, sum(iif(sex='F',1,0)) as 
females 
from somefile
***************
There is a cleaner and more easily-optimized way (when using indexes on
large tables, this will be significantly faster):
SELECT sex,  COUNT(userId) AS sexCount
FROM users
GROUP BY sex
Then, you will get two rows back:
sex | sexCount
M | (number)
F | (number)