-- Using IN --
select c.*
from customers as c
where c.shipping_detail_id IN
( select sd.shipping_detail_id
from shipping_details as sd
where sd.province = 'NS'
) ;
-- Using EXISTS --
select c.*
from customers as c
where EXISTS
( select 1
from shipping_details as sd
where sd.province = 'NS'
and sd.shipping_detail_id = c.shipping_detail_id -- correlation
) ;
由于您不需要从 shipping_details 表中返回任何数据,因此可以使用semi-join,它可以使用
IN
和子查询或EXISTS
相关子查询来编写。例子: