I need to return the count of item sold in last 30 days and return 0 if there is no item sold. My current query is working well for getting days where there is item sold but it is not returning 0 values and I need it.
Here is my current query
SELECT DATE_FORMAT(FROM_UNIXTIME(purchase_date), '%m/%d') AS Dates, COUNT(*) AS COUNT
FROM purchases
WHERE FROM_UNIXTIME(purchase_date) BETWEEN NOW() - INTERVAL 30 DAY AND NOW()
GROUP BY Dates
ORDER BY Dates ASC
The actual result are:
02/15 -> 10
02/16 -> 12
02/18 -> 22
But I'm missing the empty date(s) that I want to return as 0.
This is the table structure:
userid int(11), purchase_item int(11), purchase_date int(11)