2014-11-21 63 views
0

我有一张医生表和一张位置表。每位医生可以有多个地点,但每个医生只能将一个地点标记为默认地点。我试图找到没有标记为默认位置的医生。这个连接是doctors.ID到locations.doctor_id。SQL找到没有标记为默认的链接记录

回答

1

您可以使用not exists子句。

select * from doctor d 
where not exists 
(select 1 from locations 
    where locations.doctor_id = d.id 
    and locations.default =1 
) 

另一种方法是使用做left join

select * from doctor d 
left join locations l 
on l.doctor_id = d.id 
and l.default =1 
where l.doctor_id is NULL 
相关问题