【发布时间】:2014-06-10 15:15:41
【问题描述】:
我有以下查询:
select distinct a.id, a.name
from Employee a
join Dependencies b on a.id = b.eid
where not exists
(
select *
from Dependencies d
where b.id = d.id
and d.name = 'Apple'
)
and exists
(
select *
from Dependencies c
where b.id = c.id
and c.name = 'Orange'
);
我有两张桌子,比较简单。 第一个 Employee 有一个 id 列和一个 name 列 第二个表 Dependencies 有 3 列,一个 id、一个 eid(要链接的员工 id)和名称(apple、orange 等)。
数据看起来像这样 员工表是这样的
id | name
-----------
1 | Pat
2 | Tom
3 | Rob
4 | Sam
依赖关系
id | eid | Name
--------------------
1 | 1 | Orange
2 | 1 | Apple
3 | 2 | Strawberry
4 | 2 | Apple
5 | 3 | Orange
6 | 3 | Banana
如您所见,Pat 同时拥有 Orange 和 Apple,他需要被排除在外,并且必须通过连接,而我似乎无法让它工作。最终数据应该只返回 Rob
【问题讨论】:
-
用简单的英语写下您的要求。我必须阅读您的代码才能尝试猜测您的要求。