2016-03-31 4 views
0

Ich habe 3 Tabellen A, B und C. Tabelle A enthält Produktdetails ohne Preisspalte. Tabelle B und C enthält Produktdetails mit Preisspalte darin.Abfragen von 3 Tabellen durch Vergleichen von Spaltenwerten und ohne Primär- oder Fremdschlüsselbeziehung

Das Produkt in der Tabelle A kann oder nicht vorhanden sein in Tabelle B oder C.

Ich mag den Mindestpreis des Produkts von Tabelle A erhalten, wenn es in Tabelle B oder C ist, und wählen Sie die mininum Preis von B oder C

Lets sagen Tabelle A hat Property1, Property2 und property3


Tabelle B Preis, Property1, Property2 und property3


Tabelle C hat Preis, Property1, Property2 und property3

die minimun Preis für Artikel aus der Tabelle B Erhalten oder C je nachdem, was Preis ist, wo A.properties Spiele mit Eigenschaften von B und C

Wie ich tun frage das ab.

+0

Posting Beispieldaten werden viel hilfreicher für alle – mohan111

+3

MySQL oder SQL-Server sein? –

Antwort

0

Sie könnten Daten aus der Tabelle b und c kombinieren mit union all, dann eine inner join Tabelle ein verwenden, kombiniert mit einem min() und group by den Mindestpreis zu bekommen.

select a.property1, a.property2, a.property3, min(x.price) as min_price 
from table_a a 

-- join to combined data from table b and table c 
inner join (
    select price, property1, property2, property3 
    from table_b 
    union all 
    select price, property1, property2, property3 
    from table_c 
) x 
-- join on properties 
on a.property1= x.property1 
and a.property2= x.property2 
and a.property3= x.property3 
) 
group by a.property1, a.property2, a.property3 
1

Try this:

SELECT big.prop1, big.prop2, MIN(big.price) 
FROM (
    SELECT a.prop1, a.prop2, b.price 
    FROM TableA a 
    INNER JOIN TableB b ON a.prop1 = b.prop1 AND a.prop2 = b.prop2 

    UNION 

    SELECT a.prop1, a.prop2, c.price 
    FROM TableA a 
    INNER JOIN TableC c ON a.prop1 = c.prop1 AND a.prop2 = c.prop2 
) AS Big 
GROUP BY big.prop1, big.prop2 

Aber, das ist eine schlechte Praxis, müssen Sie Fremdschlüssel für jede Kombination og Eigenschaften Tabellen zu verknüpfen.

+0

Dies wird Datensätze von a zurückgeben, auch wenn sie nicht in b oder c existieren. Siehe unten meine Lösung, die keine Datensätze von a zurückgibt, wenn sie weder in a noch in b existiert. –

+0

THX für Beratung, behoben – D0dger

+0

@ JoshGilfillan-Danke, ich wollte noch eine Sache überprüfen, wenn ich hier ähnliche Bedingungen stellen kann. Bei a.property1 handelt es sich um "Samsung Mobile" und bei c.prop1 um "Samsung", also gibt es eine Möglichkeit wie ich hier ein setzen kann. a.prop1 wie c.prop1. – John

0
create table #a (Property varchar(10)) 
INSERT INTO #A VALUES ('X') 
INSERT INTO #A VALUES ('D') 
INSERT INTO #A VALUES ('I') 

create table #b (price int,Property varchar(10)) 
INSERT INTO #B VALUES (10,'X') 
INSERT INTO #B VALUES (11,'D') 
INSERT INTO #B VALUES (12,'I') 

create table #c (price int ,Property varchar(10)) 

INSERT INTO #C VALUES (8,'X') 
INSERT INTO #C VALUES (9,'D') 
INSERT INTO #C VALUES (7,'I') 

SELECT B.PROPERTY,CASE WHEN MIN(B.PRICE) < MIN (C.PRICE) THEN B.PRICE ELSE C.PRICE END AS PRICE 
FROM #B B INNER JOIN #C C ON B.Property=C.Property INNER JOIN #A A ON B.Property=A.Property 
GROUP BY B.PRICE,C.PRICE,B.PROPERTY 

DROP TABLE #A 
DROP TABLE #B 
DROP TABLE #C 
+0

Kopieren Sie diese Abfrage und führen Sie sie in SQL aus, um die Ausgabe zu verstehen. – ranjan0487

+0

Danke Felix für die Bearbeitung. – ranjan0487

Verwandte Themen