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
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
GROUP BY sex
Then, you will get two rows back:
sex | sexCount
M | (number)
F | (number)