The typical approach here is to first write a query that gets the list of dates you want, then use an outer join to associate the transaction amounts for the dates in that range on which there were transactions.
My recommendation is to install common_schema and use the common_schema.numbers table to generate the date list.
For example you can run a query like this to get the last 30 days (exclusive of today):
select (current_date() - interval n day) as day
from common_schema.numbers
where n between 1 and 30
order by day
Then you can combine that with your existing query to get the desired result (I made some other small changes to your query to limit it to the relevant date range and to use DATE()
instead of DATE_FORMAT()
for the sake of simplicity):
select days.day, coalesce(transactions_rollup.total,0)
from
(
select (current_date() - interval n day) as day
from common_schema.numbers
where n between 1 and 30
) days
left outer join (
select date(transaction_date) as day, sum(amount) as total
from transactions
where transaction_date >= current_date() - interval 30 day
and transation_date < current_date()
group by date(transaction_date)
) transactions_rollup on transactions_rollup.day = days.day
order by days.day