mysql - Populate empty dates -
my database table structure like
stats_id stats_date stats_visitors stats_visits 1 2015-08-01 10 20 2 2015-08-03 12 21 3 2015-08-04 14 24 4 2015-08-07 15 21
what easiest way add empty stats date. example missing 2015-08-02 should be:
stats_id stats_date stats_visitors stats_visits 1 2015-08-01 10 20 5 2015-08-02 0 0 2 2015-08-03 12 21 3 2015-08-04 14 24 6 2015-08-05 0 0 7 2015-08-06 0 0 4 2015-08-07 15 21
i can check every day if has data , populate when have on 10k rows not idea. if use check each day generate on 10k queries.
is there easy way can this?
this should out.
insert table1 select '',selected_date,'0','0' (select adddate('1970-01-01',t4.i*10000 + t3.i*1000 + t2.i*100 + t1.i*10 + t0.i) selected_date (select 0 union select 1 union select 2 union select 3 union select 4 union select 5 union select 6 union select 7 union select 8 union select 9) t0, (select 0 union select 1 union select 2 union select 3 union select 4 union select 5 union select 6 union select 7 union select 8 union select 9) t1, (select 0 union select 1 union select 2 union select 3 union select 4 union select 5 union select 6 union select 7 union select 8 union select 9) t2, (select 0 union select 1 union select 2 union select 3 union select 4 union select 5 union select 6 union select 7 union select 8 union select 9) t3, (select 0 union select 1 union select 2 union select 3 union select 4 union select 5 union select 6 union select 7 union select 8 union select 9) t4) v selected_date between (select min(stats_date) table1) , (select max(stats_date) table1) , selected_date not in (select stats_date table1);
the select adddate
part of query give dates in between max , min date of data set, , using not in
dates not exist in data set.
since have auto incrementing id field, can pass empty string in select query , ignore , insert auto-incremented value.
source of select dates query: how list of dates between 2 dates in mysql select query
hope solves problem.
Comments
Post a Comment