2017-12-13 11 views
1

Ich habe 3 Tabellen Appliance, Tx_property, Rx_propertyDie Kombination von zwei Abfragen JOIN zusammen

#Appliance Table structure: 
    -id 
    -tx_property_id 
    -rx_property_id 
    -... 

#Tx_property Table structure: 
    -id 
    -... 

#Rx_property Table structure: 
    -id 
    -... 

Constraint auf gesetzt rx_property.id = appliance.id und tx_property.id = appliance.id

Ist es möglich, nur eine Abfrage ausführen, um alle Datensätze aus der Appliance-Tabelle abzurufen, die entsprechende Einträge in (basierend auf rx_property_id und tx_property_id) enthalten?

Zusammenfassend möchte ich 2 Abfragen unten in eins kombinieren.

select * from appliance INNER JOIN rx_property ON rx_property.id= appliance.rx_property_id; 
select * from appliance INNER JOIN tx_property.id ON tx_property.id= appliance.tx_property_id; 

Irgendwelche Ideen? Vielen Dank!

Edit:

Wenn ich also Beispieldatensätze wie unten haben:

appliance records: 
id = 70 
tx_property_id = 11 
rx_property_id = null 

id = 71 
tx_property_id = 12 
rx_property_id = null 

id = 72 
tx_property_id = null 
rx_property_id = 11 

id = 73 
tx_property_id = null 
rx_property_id = 12 

tx_property records: 
id = 11 
name = 'tx_aa' 

id = 12 
name = 'tx_bb' 

rx_property records: 
id = 11 
name = 'rx_aa' 

id = 12 
name = 'rx_bb' 

Ich will 4 von ihnen abgerufen werden wie:

appliance.id = 70 
appliance.tx_property_id = 11 
appliance.rx_property_id = null 
tx_property.id = 11 
tx_property.name = 'tx_aa' 

appliance.id = 71 
appliance.tx_property_id = 12 
appliance.rx_property_id = null 
tx_property.id = 12 
tx_property.name = 'tx_bb' 

appliance.id = 72 
appliance.tx_property_id = null 
appliance.rx_property_id = 11 
tx_property.id = 11 
tx_property.name = 'rx_aa' 

appliance.id = 73 
appliance.tx_property_id = null 
appliance.rx_property_id = 12 
tx_property.id = 11 
tx_property.name = 'rx_aa' 

Antwort

0

Sie können beide Tabellen appliance in einer einzigen Abfrage beitreten:

select * 
from appliance a 
join rx_property r on r.id = a.rx_property_id 
join tx_property t on t.id = a.tx_property_id 

Die obige Abfrage gibt nur diese Zeilen, die in beiden Tabellen verweisen. Wenn Sie alle Zeilen erhalten möchten, verwenden Sie left join:

select * 
from appliance a 
left join rx_property r on r.id = a.rx_property_id 
left join tx_property t on t.id = a.tx_property_id; 

id | rx_property_id | tx_property_id | id | name | id | name 
----+----------------+----------------+----+-------+----+------- 
70 |    11 |    | 11 | rx_aa | | 
71 |    12 |    | 12 | rx_bb | | 
72 |    |    11 | |  | 11 | tx_aa 
73 |    |    12 | |  | 12 | tx_bb 
(4 rows)  

Sie coalesce() in der Auswahlliste verwenden, können nicht null Spalte, beispielsweise zur Auswahl:

select 
    a.id, rx_property_id, tx_property_id, 
    coalesce(r.name, t.name) as property_name 
from appliance a 
left join rx_property r on r.id = a.rx_property_id 
left join tx_property t on t.id = a.tx_property_id 

id | rx_property_id | tx_property_id | property_name 
----+----------------+----------------+--------------- 
70 |    11 |    | rx_aa 
71 |    12 |    | rx_bb 
72 |    |    11 | tx_aa 
73 |    |    12 | tx_bb 
(4 rows)  

Beachten Sie auch, dass ich Aliase für Tabellen verwendet haben was im Allgemeinen Abfragen lesbarer macht.

Lesen kommen mehr über Typen in the documentation.

0

Es ist genauso einfach wie das :

select * from appliance 
INNER JOIN rx_property ON rx_property.id= appliance.rx_property_id 
INNER JOIN tx_property ON tx_property.id= appliance.tx_property_id 

Sie können beliebig viele Joins zu einer einzelnen SELECT-Anweisung hinzufügen.

+0

ich dies bereits versucht haben, ich bin immer 0 Zeilen zurückgegeben. Wenn ich obige Abfragen getrennt ausführe, bekomme ich korrekt Zeilen zurückgegeben. – Pawel

+0

Das liegt daran, dass Sie keine Datensätze haben, die alle Anforderungen erfüllen. Also passend zu rx_property_id und tx_property_id in allen verwendeten Tabellen. – cybernetic87

+0

Ich denke, es gab ein Missverständnis. Ich habe die Frage aktualisiert, bitte sehen Sie. – Pawel