不确定要使用哪个SQL连接

时间:2013-10-11 14:24:40

标签: mysql sql join

我有2个表(AllClients& AllActivities),需要检索以下信息:

我需要一个没有相关活动的客户列表。

以下是我的表格,其中包含一些虚假数据,以帮助解释我正在寻找的内容:

http://www.givingdirection.com/table.jpg

我基本上需要sql来检索Sam Johnson,因为他没有活动,而且他的TypeCode为'P'。

4 个答案:

答案 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)
相关问题