2013-01-16 65 views
0

我試圖將一個表連接到另外兩個表上,並將它們引用到連接表中。例如:SQL - 將一個表連接到另外兩個表

table1 = order_header 
table2 = order_line 
table3 = inventory 

,所以我需要得到具體的訂單,所以我需要看order_header和ORDER_LINE,也希望檢索庫存記錄可能不存在(這是我覺得外部聯接)。問題是,我可以看到庫存是否存在的唯一方法是同時查看order_header和order_line以匹配庫存。

order_header.location = inventory.location 
order_line.product = inventory.product 

但我可以外部加入庫存引用order_header和order_line細節?

對不起,解釋不好,但任何幫助,將不勝感激。

謝謝。

Gillian。

回答

0

試試這個:

SELECT * FROM order_header 
INNER JOIN order_line ON order_header.header_id = order_line.header_id 
LEFT OUTER JOIN inventory ON order_line.product = inventory.product AND order_header.location = inventory.location 
WHERE order_header.header_id = xxx 

它的工作原理是因爲第一內order_header之間的連接,然後ORDER_LINE可以被視爲一個單獨的表,所以你剛剛離開加入對 - 那種虛構 - 表

+0

托馬斯你的英雄!工作過一種享受。先沒有想到內連接。 –