RE: [PHP] Show total records based on criteria
| From: | Jay Blanchard | Date: | Wed, 12 Nov 2003 13:25:16 +0000 |
| Subject: | RE: [PHP] Show total records based on criteria | ||
| Groups: | php.general | ||
| Request: | Send a blank email to php-general+get-169266@lists.php.net to get a copy of this message | ||
[snip]
For example, if I create a record set of all male employees regardless
of eye color and then I want to create a table showing the totals of
people of each eye color. Is there a way to do this with out creating
several different recordsets.
[/snip]
Yes, you handle it in the query;
$sql = "SELECT COUNT(eyeColor) AS colorCount, eyeColor FROM theTable
WHERE sex = 'male' GROUP BY eyeColor ";
You can also do a cross-tab query;
$sql = "SELECT eyeColor, ";
$sql .= "SUM(IF(sex = 'male', 1, 0)) AS Guys, ";
$sql .= "SUM(IF(sex = 'female', 1, 0)) AS Gals ";
$sql .= "FROM theTable ";
$sql .= "GROUP BY eyeColor ";