2017-04-06 4 views
-1

나는 며칠간 풀리지 않은 폴링 문제가 있습니다.테이블에서 결합 된 데이터를 얻으십시오

3 개의 테이블이 있습니다. 어떤 제품을 제공 할 수있는 대리점 -

ID | Product 
1 | Apple 
2 | Peache 
3 | Banana 
4 | Lemon 

표 "DistributorProduct"

표 "대리점"

ID | Distributor 
1 | Distributor A 
2 | Distributor B 
3 | Distributor C 

표 "제품".

ID | DistributorID | ProductID 
1 | 1 | 1 
2 | 1 | 2 
3 | 1 | 3 
4 | 1 | 4 
5 | 2 | 1 
6 | 2 | 3 
7 | 2 | 4 
7 | 3 | 3 
7 | 4 | 4 

이제 Apple AND Banana를 제공 할 수있는 모든 디스트리뷰터를 얻고 싶습니다. (결과는 배급 자 A와 배급 자 B 여야합니다).

어떻게하면됩니까? SQL 문이란 무엇입니까?

도움을 주셔서 감사합니다.

디터

+0

에 오신 것을 환영합니다 스택 오버플로! 질문 [편집]하여 [지금까지 시도한 내용] (http://whathaveyoutried.com)을 보여주십시오. 문제가있는 코드의 윤곽선 (적어도 선호하는 [mcve])을 포함해야합니다. 그런 다음 특정 문제를 해결할 수 있습니다. 또한 [ask]를 읽어야합니다. –

답변

0
select d.* 
from Distributor d 
left join DistributorProducts dp on dp.DistributorId = d.id 
left join Product p on p.Id = dp.ProductId and p.product = 'Apple' and  
p.product = 'Banana' 

일부 표현의 오류에 가입하면 가입없이 다음 당신이 할 수 있습니다.

select d.* 
from Distributor d, DistributorProducts dp, Product p 
where dp.DistributorId = d.id 
and p.Id = dp.ProductId 
and (p.product = 'Apple' AND p.product = 'Banana'); 

나는 이것이 효과가있을 것이라고 확신한다.

+0

그건 작동하지 않습니다. 오류 메시지 : "JOIN식이 지원되지 않습니다." – derdieter

+0

@derdieter : 다른 솔루션으로 업데이트되었습니다. 확인해 봐. –

+0

해결책을 얻으면 나는 빈 결과를 얻습니다. 문제는 두 번째 "AND"인 것 같습니다. "및 p.product = 'Banana';" 나는 "애플"로 모든 결과를 얻는다. 그러나 두 AND ('Apple'AND 'Banana') 모두 결과가 없습니다. – derdieter

0

이 작동합니다 :

select Distributor.ID, Distributor.Name 
from Distributor, Product Apple, Product Banana, DistributorProduct 
where 
    DistributorProduct.DistributorID = Distributor.ID and 
    DistributorProduct.ProductID = Apple.ID and 
    Apple.Name = 'Apple' and 
    DistributorProduct.ProductID = Banana.ID and 
    Banana.Name = 'Banana' 
group by Distributor.ID 
+0

FROM 용어로 "Product Apple, Product Banana"가있는 남성은 무엇입니까? 그리고 Apple.Name은 무엇입니까? – derdieter

+0

"Product as Apple"Apple의 짧은 별칭 양식 –

+0

Apple.Name이 Product.Name이므로 여기에서 Product의 복사본 두 개를 사용합니다. 하나는 Name = 'Apple'으로 필터링하고 두 번째는 Name = 'Banana' –