I have table with some events and their dates in a mysql database and I wanna return a counting of the events per weekday like this:
SELECT DAYNAME(start_date) AS day, count(DAYNAME(start_date)) as number FROM events GROUP BY day
That works great for days in which occur some events, but it doesn’t show the empty days. I know if I would have a simple table with days of the week I could do a simple LEFT JOIN and show ALL days, with a count = 0 when the day is empty. But is there any simpler way to do that without creating that table? Any other ideas?
Thanks in advance
In a word ‘no’. Reason being that you are trying to select data that does not exist!
However, since there are only 7 days in a week you could construct your simple ‘Days Of The Week’ table on the fly:
And then
LEFT OUTER JOINfrom that onto the events table (as you have described). This will allow you to run a query without having to create and populate the simple table.Though for the sake of reuse and tidiness I would create and populate the simple table with days of the week and use that.