2017-04-06 2 views
-1

Ich habe das folgende Problem, das ich seit Tagen nicht gelöst habe.Kombinierte Daten aus Tabelle

Ich habe 3 Tabellen.

Table "Distributor"

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

Tabelle "Produkt":

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

Table "DistributorProduct" - die Händler für das Produkt liefern kann.

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 

Jetzt möchte ich alle Distributoren, die Apple UND Banana liefern können. (Das Ergebnis muss Distributor A und Distributor B sein).

Wie kann ich das tun? Was ist die SQL-Anweisung?

Vielen Dank für jede Hilfe.

Dieter

+0

Willkommen bei Stack-Überlauf! Bitte [bearbeiten] Sie Ihre Frage, um [was Sie bisher versucht haben] (http://whathaveyoutried.com) zu zeigen. Sie sollten mindestens einen Umriss (aber vorzugsweise einen [mcve]) des Codes angeben, mit dem Sie Probleme haben, dann können wir versuchen, bei dem spezifischen Problem zu helfen. Sie sollten auch [fragen] lesen. –

Antwort

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' 

Wenn einige Ausdruck Fehler kommen, dann können Sie tun, ohne beitritt.

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'); 

Ich bin sicher, das sollte funktionieren.

+0

Das funktioniert nicht. Fehlermeldung: "JOIN Ausdruck nicht unterstützt" – derdieter

+0

@derdieter: mit einer anderen Lösung aktualisiert. prüfen Sie. –

+0

Wir Ihre Lösung bekomme ich ein leeres Ergebnis. Das Problem scheint das zweite "UND" zu sein. Ohne "und p.product = 'Banana';" Ich bekomme alle Ergebnisse mit "Apple". aber mit beiden ANDs ('Apple' UND 'Banana') gibt es keine Ergebnisse. – derdieter

0

Dies sollte funktionieren:

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

Was Männer mit "Produkt Apple, Produkt Banana" im FROM-Begriff? Und was ist Apple.Name? – derdieter

+0

Es ist kurzes Alias-Formular für "Produkt als Apple" Apple –

+0

Also Apple.Name ist Product.Name, ich verwende hier zwei Kopien des Produkts, eines gefiltert nach Name = 'Apple' und zweitens nach Name = 'Banana' –

Verwandte Themen