Forum Moderators: coopster

Message Too Old, No Replies

Mysql Query

need some help

         

d40sithui

12:28 pm on Sep 24, 2007 (gmt 0)

10+ Year Member



hey guys,
i have a table with a field named "status". its value range from 0,1,2. 0 symbolizes "new", 1 -> "approved", 2 -> "denied".
now, what i want is to form a single query and count all records where status=0, where status=1, where status=2. any ideas?

vincevincevince

12:35 pm on Sep 24, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



SELECT COUNT(*) FROM `table` GROUP BY `status`

Should do it

d40sithui

12:41 pm on Sep 24, 2007 (gmt 0)

10+ Year Member



hi,
this returns all rows. what i was looking for is to return the number of rows where status=0, the number of rows where status=1, and the number of rows where status=2

omoutop

1:48 pm on Sep 24, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



it like vincevincevince proposed:

$query1 = "SELECT status, COUNT(status) AS counted FROM {table} GROUP BY status ORDER BY status";
$result1 = mysql_query($query1) or die(mysql_error().'<p>'.$query1.'</p>');
while ($myrow1 = mysql_fetch_array($result1))
{
echo "Total Status of ".$myrow1['status']." = ".$myrow1['counted']."<br>";
}

This should give you something like:
Total Status of 0 = 15
Total Status of 1 = 57
Total Status of 2 = 2

d40sithui

2:31 pm on Sep 24, 2007 (gmt 0)

10+ Year Member



thanks.
this is perfect