The Arcive of vBulletin Modifications Site. |
|
Get Total Male/Female Members Details »»
|
|||||||||||||||||||||||||
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! Show Your Support
|
|||||||||||||||||||||||||
| Comments |
|
#2
|
||||
|
||||
|
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. |
|
#3
|
|||
|
|||
|
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? |
|
#4
|
||||
|
||||
|
Yeah, but that could also slow down the query.
|
|
#5
|
|||
|
|||
|
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? |
|
#6
|
||||
|
||||
|
Code:
$malecount = $DB_site->query_first("SELECT COUNT(*) AS total FROM userfield WHERE field19='M'");
$femalecount = $DB_site->query_first("SELECT COUNT(*) AS total FROM userfield WHERE field19='F'");
|
|
#7
|
|||
|
|||
|
Ah, brilliant - thank you very much!
That's kinda what I was trying to do, but you do it so much better! |
|
#8
|
||||
|
||||
|
or
Code:
SELECT count(*) AS count, field9 AS gender FROM userfield WHERE field9 = 'M' or field9 = 'F' GROUP BY field9 |
|
#9
|
|||
|
|||
|
Brilliant- thanks very much for your help guys! Much appreciated.
|
|
#10
|
|||
|
|||
|
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? |
![]() |
|
|