Log in

View Full Version : COUNT(column_name) MySQL Query Question



JasonDFR
10-29-2008, 06:16 PM
I have am dealing with one table named 'casting_files'. There are three 'types' of files: photos, video, text. The file type is stored in the `cf_type` column.

I want to count the number of each type of file where they all share a common `c_id` (casting id).

The code below works great, but I would like to know if there is a way to combine the queries together or shrink it in anyway while still assigning the number of each type of file to $no_photos, $no_text, and $no_video.

Thanks in advance!


$q = "SELECT COUNT(cf_type) AS 'no_photos'
FROM `casting_files`
WHERE `casting_files`.`c_id` = $c_id AND `cf_type` = 'photo' ";

$r = mysql_query($q) or die(mysql_error());

$row = mysql_fetch_array($r);
$number_photos = $row['no_photos'];
echo "$number_photos";

$q = "SELECT COUNT(cf_type) AS 'no_text'
FROM `casting_files`
WHERE `casting_files`.`c_id` = $c_id AND `cf_type` = 'text' ";

$r = mysql_query($q) or die(mysql_error());

$row = mysql_fetch_array($r);
$number_text = $row['no_text'];
echo "$number_text";

$q = "SELECT COUNT(cf_type) AS 'no_video'
FROM `casting_files`
WHERE `casting_files`.`c_id` = $c_id AND `cf_type` = 'video' ";

$r = mysql_query($q) or die(mysql_error());

$row = mysql_fetch_array($r);
$number_video = $row['no_video'];
echo "$number_video";