select *
from users u
where u.Assignable = 1
and u.UserID not in (
select UserID
from Appointments a
join TimeSlots t on a.TimeSlotID = t.TimeSlotID
where t.EndTime > now()
and t.EndTime > @desiredStartTime
and t.StartTime < @desiredEndTime
)
edit Mengambil isyarat dari tandu
Saya pikir ini juga akan berhasil, dan memiliki manfaat kinerja tambahan tanpa subkueri:
select *
from users u
left join Appointments a on a.UserID = u.UserID
left join TimeSlots t on (
a.TimeSlotID = t.TimeSlotID
and t.EndTime > now()
and t.EndTime > @desiredStartTime
and t.StartTime < @desiredEndTime
)
where
u.Assignable = 1
and t.TimeSlotID is null