I have a table with email addresses (colums: id, email, user, date). I’m trying to sum email addresses by date then user, which I’m able to do with the below code; but then also sum the total for all users and display it below that date. I’m not sure how to do that… do I need to do another query to total for each date?
$sql = mysql_query("SELECT date, COUNT(email), user FROM emails GROUP BY DATE(date), user");
while ($row = mysql_fetch_array($sql)){
echo date('m/d', strtotime($row['date'])) . " " . $row['user'] . " " . $row['COUNT(email)'] . "<br />";
}
What I have:
date user count(email)
09/09 29 8
09/09 49 9
09/10 29 4
09/10 49 13
09/11 29 1
09/11 49 3
What I would like:
date user count(email)
09/09 29 8
09/09 49 9
09/09 total 17
09/10 29 5
09/10 49 13
09/10 total 18
09/11 29 1
09/11 49 3
09/11 total 4
Thanks
EDIT: Here’s my code that works:
$sql = mysql_query("SELECT date, COUNT(email), user FROM emails GROUP BY DATE(date), user WITH ROLLUP");
while ($row = mysql_fetch_array($sql)){
echo date('m/d', strtotime($row['date'])) . " " . (!isset($row['user']) ? 'total' : $row['user']) . " " . $row['COUNT(email)'] . "<br />";
}
See
WITH ROLLUP(aGROUP BYmodifier).would give you: