2016-10-19 3 views

답변

0

. 그런 다음 조인 만하면됩니다. SupplyParts으로 두 번 가입해야합니다. 한 번은 Apple 제품을 찾고 두 번째 제품은 비 Apple 제품을 제외합니다. LEFT JOIN 당신이 ON 조항이 아닌 WHERE 절에 두 번째 테이블의 제한을 넣어

SELECT distinct c.name, c.province 
FROM Customer AS c 
JOIN Supply AS s1 ON s1.cid = c.cid 
JOIN Parts AS p1 ON p1.pid = s1.pid 
LEFT JOIN Supply AS s2 ON s2.cid = c.cid 
LEFT JOIN Parts AS p2 ON p2.pid = s2.pid AND p2.producer != 'Apple' 
WHERE p1.producer = 'Apple' AND p2.pid IS NULL 

알 수 있습니다. 이 부분에 대한 자세한 내용은 Return row only if value doesn't exist을 참조하십시오.

0

Apple 제품만을 구매하신 고객이 필요하십니까?

한 가지 가능한 솔루션은 조건 집합을 기반으로합니다 : 당신은 어떤 비 애플 제품을 구입하지 않은 고객을 찾기 위해 LEFT JOIN/NULL 패턴을 사용할 수 있습니다

Select c.cname, c.Province 
From Customer c 
join 
(-- this is not a Subquery, it's a Derived Table 
    Select s.CId -- assuming there's a CId in Supply 
    from Supply s 
    join Part p 
    on p.pId = s.pId 
    group by s.CId 
    -- when there's any other supplier this will return 1 
    having max(case when p.Producer = 'Apple' then 0 else 1 end) = 0 
) as p 
on p.CId = c.CId 
관련 문제