P粉7331667442023-08-25 00:59:49
When I needed to deal with this problem, to fill in the missing dates, I actually created a reference table containing just all the dates I was interested in and joined the data tables on the date fields. It's crude and crude, but it works.
SELECT DATE(r.date),count(d.date) FROM dates AS r LEFT JOIN table AS d ON d.date = r.date GROUP BY DATE(r.date) ORDER BY r.date ASC;
As for the output, I would use SELECT INTO OUTFILE instead of manually generating the CSV. This way we don't have to worry about escaping special characters.
P粉5936497152023-08-25 00:39:00
When you need something similar on the server side, you usually create a table containing all possible dates between two points in time, and then do a left join to this table with the query results. Something similar to the following:
create procedure sp1(d1 date, d2 date) declare d datetime; create temporary table foo (d date not null); set d = d1 while d <= d2 do insert into foo (d) values (d) set d = date_add(d, interval 1 day) end while select foo.d, count(date) from foo left join table on foo.d = table.date group by foo.d order by foo.d asc; drop temporary table foo; end procedure
In this specific case, it would be better to do some check on the client side and add some additional string if the current date is not the day after the previous day.