I have 3 tables named
- com_event_schedules
- com_appointments
- com_event_schedules_com_appointment_c
which has a relation between first two tables.
Following are the fields of the tables
- com_event_schedules -- id -- name -- schedule_date -- start_time -- end_time -- deleted
- com_appointments -- id -- start_time -- end_time -- status
- com_event_schedules_com_appointment_c -- id -- com_event_schedules_com_appointmentcom_event_schedules_ida (schedule_id) -- com_event_schedules_com_appointmentcom_appointment_idb (appointment_id)
relation between tables com_event_schedule and com_appointments is 1 to Many
What I want in result schedule_id, and total counts of its appointments who's status='completed'
I tried following query:
SELECT sch.id,COUNT(app.status)
FROM
com_event_schedules sch,
com_appointment app,
com_event_schedules_com_appointment_c rel
WHERE
sch.id=com_event_schedules_com_appointmentcom_event_schedules_ida AND
app.id=rel.com_event_schedules_com_appointmentcom_appointment_idb AND
app.status='completed'
What I'm expecting in result is id of each schedule with its COUNT of appointments which are having status "completed" and COUNT should be "0" if there is no match.
But What Im getting in result is Only schedule id who is having appointment with status completed and COUNT 1.
e.g
I'm having 2 schedule and for each I'm having 3 appointments. Out of which 1st schedule's one appointment is having status='completed'
So I need result like this
sch_id app.status
1 1
2 0