views:

39

answers:

2

I have a table: id, datetime, event

i also have table dates: date (Y-m-d format)

the problem is some days don't have any events, I would like them to show 0 (or null)

SELECT DATE_FORMAT(table.timestamp, '%Y-%m-%d') ydm, count(table.fkUID) FROM `table`  where table.fkUID=$var group by ydm;

is there some way to join or use conditional statements to make the result show:

date|count
----------
2010-05-23| 5
2010-05-24| 0  <--- this line just doesn't exist in my query.
2010-05-26| 3
+2  A: 
select d.date,count(e.timestamp)
from dates d
left join events e on d.date=DATE_FORMAT(e.timestamp,'%Y-%m-%d')
group by d.date;

I don't have environment for testing, and I'm not sure that I am clear about the question, but here it is my guess.

Notinlist
The left join will make sure that no row will be missed from the dates table. The events will be counted by date.
Notinlist
I tried this, it didn't add in zeroes where there was no events timestamped that day. It was much cleaner than my existing code though.
Kevin Ohashi
Note that you could also use `d.date = DATE(e.timestamp)`.
Marcus Adams
A: 

The accepted wisdom seems to be that you have to create a table containing a list of all the dates you're interested in and then do an outer join on with the view you have there. Sucks, I know.

Malvolio