You'll need to create a helper table and fill it with all dates from start
to end
, then just LEFT JOIN
with that table:
SELECT d.dt AS date,
COUNT(*) AS total,
SUM(attitude = 'positive') AS positive,
SUM(attitude = 'neutral') AS neutral,
SUM(attitude = 'negative') AS negative
FROM dates d
LEFT JOIN
messages m
ON m.posted_at >= d.dt
AND m.posted_at < d.dt + INTERVAL 1 DAYS
AND spam = 0
AND duplicate = 0
AND ignore = 0
GROUP BY
d.dt
ORDER BY
d.dt
Basically, what you need here is a dummy rowsource.
MySQL
is the only major system which lacks a way to generate it.
PostgreSQL
implements a special function generate_series
to do that, while Oracle
and SQL Server
can use recursion (CONNECT BY
and recursive CTE
s, accordingly).
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…