I have an array from this mysql query:
SELECT COUNT(*) as opens, HOUR(FROM_UNIXTIME(open_date)) as hour
FROM tracking WHERE open_id = 10 GROUP BY HOUR(FROM_UNIXTIME(open_date))
It allows me to get opens based on every hour. However, there are some gaps in the hours as nothing has been opened at a particular hour. How can I fill the gaps i.e. turn this array:
Array (
[0] => Array ( [opens] => 9 [hour] => 0 )
[1] => Array ( [opens] => 2 [hour] => 3 ) )
Into:
Array (
[0] => Array ( [opens] => 9 [hour] => 0 )
[1] => Array ( [opens] => 0 [hour] => 1 )
[2] => Array ( [opens] => 0 [hour] => 2 )
[3] => Array ( [opens] => 2 [hour] => 3 ) )
In other words fill missing hours with 0 opens? Can I change my sql query to get this to work or will it have to be via PHP?
Thanks all for any help.
You could do it in code based on the results that you get back. One way to do it using only the database would be to have a table (maybe even a temporary one) that contains the hours and do a left outer join on
hoursvs.tracking. Note that this is practical only because the range forhoursis fixed and relatively small — basically a single column containing the numbers 0-23.