I have the following query:
SELECT timestamp,
COUNT(*)
FROM table
GROUP BY timestamp
But some timestamps do not show up because there is no data. Here’s an example
1:00:00 | 3
1:00:02 | 17
1:00:03 | 2
Notice that 1:00:01 is missing. Is there a way to make the 1:00:01 | 0 appear in the result?
MySQL doesn’t have recursive functionality, so you’re left with using the NUMBERS table trick –
Create a table that only holds incrementing numbers – easy to do using an auto_increment:
Populate the table using:
…for as many values as you need.
Use DATE_ADD to construct a list of times, increasing the seconds based on the NUMBERS.id value:
LEFT JOIN onto your table of data based on the time portion: