I am writing a reporting program which is using MySQL as a database and php as scripting language.
What i'm trying to do is to group records by time and group them in hours having last 24 hours in the result set.
Current Query
SELECT count(*),time FROM `service_data` GROUP BY time
the query above does not group them by last 24 hours it just groups similar unixtimestamp together
Table Structure
CREATE TABLE IF NOT EXISTS `service_data` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`service_id` varchar(255) NOT NULL,
`ip_address` varchar(255) NOT NULL,
`time` int(11) NOT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1 AUTO_INCREMENT=5 ;
In the result set i want 24 records null if no reports in a particular hour.
Like so
| hour | reports |
|------|----------|
| 1 | 5 |
| 2 | 10 |
| 3 | 12 |
| 4 | 25 |
Any help will be much appreciated!
Thanks!