for instance if I have a table like this
total date
10 2010-01-01
15 2010-01-02
98 2010-01-03
50 2010-01-05
how can I write select statement to get output like this?
total date
10 2010-01-01
15 2010-01-02
98 2010-01-03
0 2010-01-04
50 2010-01-05
MySQL doesn’t have recursive functionality, so you’re left with using the
NUMBERStable 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. In this case, the INSERT statement needs to be run at least 31 times.
Use DATE_ADD to construct a list of days, increasing based on the
NUMBERS.idvalue:Use an OUTER JOIN to get your desired output:
Why Numbers, not Dates?
Simple – dates can be generated based on the number, like in the example I provided. It also means using a single table, vs say one per data type.