I have two tables: “rota” (1) and “volunteer” (2). The first name and last name columns are in (2), but the shifts in (1). With the following query I am able to see the names of those who have submitted their shifts, but I would like to see those who have not. Can’t figure it out how. Any help?
SELECT COUNT(rota.shift_id), rota.volunteer_id, volunteer.firstname, volunteer.lastname
FROM rota
INNER JOIN volunteer ON rota.volunteer_id=volunteer.id
GROUP BY volunteer_id;
2
Answers
This was the solution:
You want a
LEFT JOIN
— and to put thevolunteer
table first because you want all the rows from that table:Note that you do not need to include all columns from
volunteer
in theGROUP BY
becausev.id
uniquely identifies each row in the result set.EDIT:
If you want volunteers with no shifts, then use
NOT EXISTS
:You can equivalently do this with
LEFT JOIN
: