[thelist] Better way to do this (SQL)?
Burhan Khalid
thelist at meidomus.com
Tue May 18 03:42:08 CDT 2004
Hello everyone :
A poster asked on php.general :
"I have collected a few demographics from certain respondents. These are
sex, age, and usage of our service (individual/company). The responses
are store in a mysql 3 database.
Basically, I would like to show:
total headcounts and percentages for 'male' and 'female' respondents
(database column 'sex')"
To which I responded with this possible solution:
Here is one way:
$allheads = mysql_num_rows(mysql_query("SELECT sex FROM table"));
$allmales = mysql_num_rows(mysql_query("SELECT sex FROM table WHERE sex
= 'male'"));
$allfemales = mysql_num_rows(mysql_query("SELECT sex FROM table WHERE
sex = 'female'"));
$avg_male = ($allmales / $allheads) * 100;
$avg_female = ($allfemales / $allheads) * 100;
(probably not the best)
But I'm wondering if there is a way to do this using one query?
Thanks for the education :)
Burhan
More information about the thelist
mailing list