You can try one of the following:
select employee
from personnel
where building in (A, B)
having max(building) = min(building) and min(building) = A;
or:
select employee
from personnel
where building = A and
not exists (select 1
from personnel p2
where p2.employee = p.employee and p2.building = B
);
In particular, this can use indexes on personnel(building, employee)
and personnel(employee, building)
.
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…