2

I need to get data for all hours of day, even if the count is 0. Right now it outputs:

clicks--hour
 1----- 7
 2----- 13

My query right now:

SELECT count(*) as clicks, hour(time) as hour FROM clicks WHERE DATE(time) = DATE(NOW()) GROUP BY hour

Thank you for your time

#

Solved, managed to do it on my own, some how.

SELECT h.Hour
     , COUNT(id) AS clicks
  FROM ( SELECT 0 AS Hour
         UNION ALL SELECT 1   
         UNION ALL SELECT 2 
         UNION ALL SELECT 3 
         UNION ALL SELECT 4 
         UNION ALL SELECT 5 
         UNION ALL SELECT 6 
         UNION ALL SELECT 7 
         UNION ALL SELECT 8 
         UNION ALL SELECT 9 
         UNION ALL SELECT 10 
         UNION ALL SELECT 11
         UNION ALL SELECT 12
         UNION ALL SELECT 13
         UNION ALL SELECT 14
         UNION ALL SELECT 15
         UNION ALL SELECT 16
         UNION ALL SELECT 17
         UNION ALL SELECT 18
         UNION ALL SELECT 19
         UNION ALL SELECT 20
         UNION ALL SELECT 21
         UNION ALL SELECT 22
         UNION ALL SELECT 23) AS h
LEFT OUTER
  JOIN clicks
    ON hour(time) = h.Hour
   AND DATE(time) = date(now())
GROUP 
    BY h.Hour
user2770029
  • 722
  • 1
  • 7
  • 8
  • possible duplicate of [MySQL how to fill missing dates in range?](http://stackoverflow.com/questions/3538858/mysql-how-to-fill-missing-dates-in-range) – Jeremy Smyth Sep 15 '13 at 13:33
  • I tried to google before asking here and all the answers are too complicated for me, sorry. – user2770029 Sep 15 '13 at 13:35

1 Answers1

0

You can use the COALESCE This command helps in the case that a select returns null, to replace null with a value like this:

SELECT 
    COALESCE(count(*), 0) as clicks, 
    COALESCE(hour(time), hour(now())) as hour 
FROM 
    clicks 
WHERE DATE(time) = DATE(NOW()) GROUP BY hour

Hope that's what you're looking for.

Not Amused
  • 942
  • 2
  • 10
  • 28