你如何加入Oracle中的三个表?

时间:2014-02-03 16:42:19

标签: sql oracle join inner-join

我很难加入Oracle中的三个单独的表格。我之前从未参加过三张桌子,所以我并不精通。我的理论如下:

SELECT customer_num WHEN customer_num IS 104 -198, order_num
  FROM orders INNER JOIN items
    ON order_num, stock_num
    INNER JOIN stock
      ON stock_num, description

基本上,我正在尝试从ORDERS表开始,并从订单表中提取客户编号(customer_num),特别是客户编号104 -108order_num 。然后将订单表附加到Items表并附加order_numstock_num,最后附上stock表并提取stock_num和说明。

3 个答案:

答案 0 :(得分:1)

您的语法遍布整个地方,不适用于双表连接,甚至不能查询单个表。不知道你从哪里获得WHEN,或者你的条款的顺序。请查看SQL reference,了解如何加入以及如何过滤。总之...

你似乎想要这样的东西:

SELECT o.customer_num, o.order_num, i.stock_num, s.description
FROM orders o
INNER JOIN items i ON i.order_num = o.order_num
INNER JOIN stock s ON s.stock_num = i.stock_num
WHERE o.customer_num BETWEEN 104 AND 198;

WHERE子句正在应用于orders表,以限制您获得的客户订单。我假设您的说明中ordersitems表格中有一个共同的order_num列,您可以参与其中;并且itemsstock表格中包含您可以加入的常见stock_num列。

正如OldProgrammer所说,将表格模式包含在问题中是有帮助的,因此不需要做出假设,并且显示样本数据和该数据的预期输出将阐明您正在尝试做什么。 / p>

答案 1 :(得分:0)

看起来应该是这样的(基于有关架构的有限信息)......

SELECT 
    O.CUSTOMER_NUM, 
    O.ORDER_NUM, 
    S.STOCK_NUM,
    S.DESCRIPTION
FROM 
    ORDERS O,
    ITEMS I,
    STOCK S
WHERE
    I.CUSTOMER_NUM >= 104
    AND I.CUSTOMER_NUM <= 198
    AND O.ORDER_NUM = I.ORDER_NUM
    AND I.STOCK_NUM = S.STOCK_NUM

答案 2 :(得分:0)

JOIN的语法是:

TableExpression [ INNER ] JOIN TableExpression
{
  ON booleanExpression |    USING clause
}

booleanExpression是表将链接的内容。所以,在你的例子中,像这样:

SELECT customer_num WHEN customer_num IS 104 -198, order_num
FROM orders INNER JOIN items
  ON orders.<some_column> = items.<some_column>
INNER JOIN stock
  ON items.<some_column> = stock.<some_column>