I want a to print something that looks like this:
date - kcal
2015/8/8 - 1700
2015/8/9 - 1785
2015/8/10 - 1822
So I want to group all records on the same date, but they are saved as timestamps in the database, so I need to convert them to just the date (no time) before grouping them, then sum up all kcal fields on the same date.
I'm having trouble getting my head around the syntax. This is what I have:
$query = mysql_query("SELECT DATE(thedate), SUM(kcal) FROM food_records GROUP BY DATE(thedate) ORDER BY thedate");
$result = mysql_query($query) or die(mysql_error());
while($row = mysql_fetch_array($result)){
echo $row['thedate']. " - ". $row['SUM(kcal)'];
echo "<br />";
}
Here is my SHOW CREATE TABLE
CREATE TABLE `food_records` (
`record_id` int(11) NOT NULL AUTO_INCREMENT,
`thedate` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP,
`item_id` varchar(5) NOT NULL,
`kcal` int(4) NOT NULL,
PRIMARY KEY (`record_id`)
) ENGINE=MyISAM AUTO_INCREMENT=37 DEFAULT CHARSET=utf8
And here is SHOW VARIABLES LIKE "%version%"
protocol_version 10
version 5.1.73
version_comment Source distribution
version_compile_machine x86_64
version_compile_os redhat-linux-gnu