2013-01-16 156 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

托马斯你的英雄!工作过一种享受。先没有想到内连接。 –