You can use an except
to get all those where attended = 0
except those where attended = 1
. Unfortunately MySQL doesn't support except
so we have to fake it.
For example...
select *
from attendance;
employee_id training_id attended
----------- ----------- ----------
1 1 0
1 1 1
2 1 0
3 1 1
1 2 0
1 3 1
In this example employee 1 has to make up training 2, and employee 2 has to make up training 1.
select employee_id, training_id
from attendance
where attended = 1;
employee_id training_id
----------- -----------
1 1
3 1
1 3
That is everyone who attended their training.
select employee_id, training_id
from attendance where attended = 0;
employee_id training_id
----------- -----------
1 1
2 1
1 2
That's everyone who missed a training.
Normally you'd use an except
here to run both queries.
select employee_id, training_id
from attendance
where attended = 0
except
select employee_id, training_id
from attendance
where attended = 1;
employee_id training_id
----------- -----------
1 2
2 1
But MySQL doesn't support that. Instead we emulate it with a left excluding self join.
select a1.employee_id, a1.training_id
from attendance a1
-- Self join with everyone who attended
left join attendance a2 on
a1.employee_id = a2.employee_id and
a1.training_id = a2.training_id and
a2.attended = 1
-- Get everyone who was marked as not attending and did not later attend
where a1.attended = 0
and a2.employee_id is null;