0

我正在尝试将一个表外部连接到另外两个表上,并将它们都引用到连接的表中。例如:

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 详细信息的库存吗?

抱歉解释不佳,但我们将不胜感激。

谢谢。

阿娇。

4

1 回答 1

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 之间的第一个内部连接可以被视为一个单独的表,所以你只需将连接留在那个 - 一种虚构的 - 表上

于 2013-01-16T11:01:11.003 回答