I have two tables, one called calendars and the other one called events. There can be multiple calendars, and multiple events in a calendar. I want to select every calendar, also getting the number of events in the calendar.
This is what I have :
SELECT C.*, COUNT(*) AS events FROM `calendars` AS C
LEFT JOIN `events` E ON C.ID=E.calendar
GROUP BY C.ID
But that doesn’t work. Items with no events still return 1. Any ideas?
The problem is that you´re counting rows, and a calendar that has no events, still have one row of his calendar.
Try this: