You should create a calendar table. If you're using MariaDB (v10.1 above), there's an easy way to generate a temporary calendar table. With MySQL however, you'll going to use longer query (not sure in latest MySQL version though). Refer following example:
SELECT ADDDATE('1970-01-01',t4.i*10000 + t3.i*1000 + t2.i*100 + t1.i*10 + t0.i) as dates FROM
(SELECT 0 i 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 i 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 i 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 i 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 i 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;
This will generate a date range from '1970-01-01' to '2243-10-16' in MySQL but of course you can change it accordingly. To create a calendar
table, I've prepared a fiddle here : https://www.db-fiddle.com/f/qVgmfDaVUszABAyFzH7iUS/3
I have also previously provide a suggestion for shorter date range here : https://stackoverflow.com/a/58443510/10910692 inclusive of MariaDB suggestion; which is much shorter query to generate dates/calendar.
Now if you don't create a calendar
table, using this method your query will look something like this:
SELECT IFNULL(B.Total,0) AS Total, A.dates AS Date FROM
(SELECT ADDDATE('1970-01-01',t4.i*10000 + t3.i*1000 + t2.i*100 + t1.i*10 + t0.i) as dates FROM
(SELECT 0 i 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 i 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 i 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 i 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 i 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) A
LEFT JOIN
(SELECT
SUM(qty) as total,
DATE(time) as date
FROM t_order
where
DATE(TIME) between date('2020-02-15') and date('2020-02-26')
GROUP BY CAST(time AS date)) B
ON A.dates=B.date
WHERE A.dates BETWEEN '2020-02-15 and '2020-02-26';
It looks ridiculously long and unnecessary. But with a calendar
table, you just need to do:
SELECT IFNULL(B.Total,0) AS Total, A.dates AS Date
FROM calendar A
LEFT JOIN
(SELECT
SUM(qty) as total,
DATE(time) as date
FROM t_order
where
DATE(TIME) between date('2020-02-15') and date('2020-02-26')
GROUP BY CAST(time AS date)) B
ON A.dates=B.date
WHERE A.dates BETWEEN '2020-02-15 and '2020-02-26';