Simple PHP & MySQL Problem - COUNT
I'm messing around with a little statistics tool. My DB looks like this:
ip | campaign_id | authorized | stamp
-----------------------------------------------------
182.74.723.1 | 1 | 0 | 12349210211
82.14.127.1 | 3 | 1 | 29834043899
I want to have a line saying:
There are "XXX" hits on Campaign with ID "X" ("XXX" were authorized, "XXX" were not authorized)
My code so far:
$query = "SELECT campaign_id, COUNT(ip) FROM stats GROUP BY campaign_id";
$result = mysql_query($query) or die(mysql_error());
// Print out result
开发者_StackOverflow中文版while($row = mysql_fetch_array($result)) {
echo "There are <b>". $row['COUNT(ip)'] ." hits</b> on campaign with <b>ID ". $row['campaign_id'] ."</b> (HERE COMES THE PART I'M LOOKING FOR)";
echo "<br />";
}
Now the problem: I couldn't find a solution to count the different values of "auth". I'd be so happy if anyone could help me out in this one, thanks in advance!
Try this sql query:
SELECT campaign_id,
COUNT(ip) as tot,
sum(authorized=0) as a0,
sum(authorized=1) as a1,
....
FROM stats GROUP BY campaign_id
精彩评论