The condition AND ss.client_booking_id=".$id_booking.'
is in the wrong place. It should be part of the join with T_STAFF_SCHEDULE
. In your current query, you get all T_STAFF_SCHEDULE that match sr.id, and all T_BOOKINGS_ADMIN
when any of its parent T_STAFF_SCHEDULE
matches $id_booking
.
Well, I don't know the meaning of every column and table, but I think it should look like this:
"SELECT *, sr.id as role_id, ss.id as scheduled_entry_id
FROM ".T_STAFF_ROLE." AS sr
LEFT JOIN ".T_STAFF_SCHEDULE." AS ss
ON sr.id = ss.staff_role_id
AND ss.client_booking_id=".$id_booking."
LEFT JOIN ".T_BOOKINGS_ADMIN." AS ba
ON ba.id = ss.admin_user_id
ORDER BY sr.id";