![]() |
Get Total Male/Female Members
I've set up a custom profile field for members to enter M for Male and F for Female. I've done similar custom fields for age groups and income, and I now want to display the totals for each group on a stats page.
Basically, I've tried to do a query that fetches info from the custom field Gender (profilefield=19) and prints a total of Male members and a total for Female. So in other words if profilefieldid19 has "M" in it, return the total "M's", and same for "F". Only because I know so little about MySQL I'm not getting very far! I'm sure it would be very easy for one of you gurus out there! |
Code:
$totals = $DB_site->query_first("SELECT SUM(IF(field19='M',1,0)) AS male,SUM(IF(field19='M',1,0)) AS female FROM userfield"); You can also do this in two COUNT queries, btw. |
Oh, I wasn't even close!!!
Thanks Firefly! I take it if I had more than two variables, say for example for the age groups where there are 4 (A, B, C & D where A=Under 18, B=18-30 etc) I would just add extra AS statements as per your answer? |
Yeah, but that could also slow down the query. :)
|
Thanks. That shouldn't be a problem because it's only for the site owners, not for general public viewing.
How else would you suggest doing it with more than 2 variables? |
Code:
$malecount = $DB_site->query_first("SELECT COUNT(*) AS total FROM userfield WHERE field19='M'"); |
Ah, brilliant - thank you very much!
That's kinda what I was trying to do, but you do it so much better! |
or
Code:
SELECT count(*) AS count, field9 AS gender |
Brilliant- thanks very much for your help guys! Much appreciated.
|
OK, next query...
How can I display the percentage of each variable? For example, if there are 4 Males and 6 Females (ie 40% Male and 60% Female), how can I automatically take the totals and display the percentages? |
All times are GMT. The time now is 03:29 PM. |
Powered by vBulletin® Version 3.8.12 by vBS
Copyright ©2000 - 2025, vBulletin Solutions Inc.
X vBulletin 3.8.12 by vBS Debug Information | |
---|---|
|
|
![]() |
|
Template Usage:
Phrase Groups Available:
|
Included Files:
Hooks Called:
|