I have a system where reservations are stored as records in one table(workshop_reservation) and sessions (the things being reserved) are stored in another table(session). Each reservation belongs to a session, and each session belongs to a workshop.
I want to make a mysql query that finds the number of reservations for each session that belongs to a certain workshop. I was trying to use count() to find the number of reservations in a given session, and this query works fine for that purpose (# is the session number):
SELECT count( r.id )
FROM workshop_reservation AS `r`
WHERE r.session_id = #
I want to figure out how to perform this query for every session. I can easily find every session_id using the following query:
SELECT s.id
FROM session AS `s`
My problem is combining these, because when any of the sessions have no reservations, no row is printed for that session. Instead, I want it to print a 0 in the count column for sessions that have no reservations. The following query works fine for sessions that have at least one reservation, but it ignores sessions that have no reservations (doesn't print a row saying 0).
SELECT count( r.id ) , r.session_id
FROM workshop_reservation AS `r`
WHERE r.session_id
IN (
SELECT s.id
FROM session AS `s`
WHERE s.workshop_id =1
)
AND r.reservation_status_id =0
GROUP BY r.session_id
How can I force it to print a row with a 0 in it in this situation?