我有2个表(AllClients& AllActivities),需要检索以下信息:
我需要一个没有相关活动的客户列表。
以下是我的表格,其中包含一些虚假数据,以帮助解释我正在寻找的内容:
我基本上需要sql来检索Sam Johnson,因为他没有活动,而且他的TypeCode为'P'。
答案 0 :(得分:2)
您不一定需要加入
select *
from AllClients c
where TypeCode = 'P'
and not exists (select 1 from AllActivities a where a.LookupCode = c.LookupCode)
答案 1 :(得分:1)
select c.*
from AllClients c
left join AllActivities a on a.LoopupCode = c.LoopupCode
where a.LoopupCode is null
and c.TypeCode = 'P'
答案 2 :(得分:1)
SELECT *
FROM
AllClients
WHERE
NOT EXISTS(SELECT 1 FROM AllActivities WHERE AllActivities.lookUpCode = AllClients.lookUpCode)
AND TypeCode = 'P'
答案 3 :(得分:0)
由于缺少IN
版本:
select *
from AllClients c
where TypeCode = 'P'
and LookupCode not in (select distinct LookupCode from AllActivities)