Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/sql/85.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
连接Oracle SQL中的3个表_Sql_Oracle_Join - Fatal编程技术网

连接Oracle SQL中的3个表

连接Oracle SQL中的3个表,sql,oracle,join,Sql,Oracle,Join,我有以下3个表格: 表A: order_number | header_id 123 | 80001 表B header_id | line_id | quantity 80001 | 10001 | 1 80001 | 10002 | 3 80001 | 10003 | 5 表C header_id | line_id | hold_price_id | released_flag 80001

我有以下3个表格:

表A:

order_number | header_id
        123  | 80001
表B

   header_id | line_id | quantity
       80001 | 10001   | 1
       80001 | 10002   | 3
       80001 | 10003   | 5
表C

   header_id | line_id | hold_price_id | released_flag
       80001 |   10001 | 2001          | Y
       80001 |   10002 | 2002          | Y
       80001 |   10003 | 2003          | N
我编写了一个查询,如下所示:

SELECT A.order_number, A.header_id, B.line_id, B.quantity, C.hold_price_id, C.released_flag
FROM Table_A a,
     Table_B b,
     Table_C c
WHERE a.header_id = b.header_id
AND   c.line_id = b.line_id
AND   a.order_number = '123';
   order_number | header_id | line_id | quantity | hold_price_id | released_flag
            123 |     80001 |   10001 | 1        | 2001          | Y
            123 |     80001 |   10002 | 3        | 2002          | Y
            123 |     80001 |   10003 | 5        | 2003          | N
我的愿望输出如下所示:

SELECT A.order_number, A.header_id, B.line_id, B.quantity, C.hold_price_id, C.released_flag
FROM Table_A a,
     Table_B b,
     Table_C c
WHERE a.header_id = b.header_id
AND   c.line_id = b.line_id
AND   a.order_number = '123';
   order_number | header_id | line_id | quantity | hold_price_id | released_flag
            123 |     80001 |   10001 | 1        | 2001          | Y
            123 |     80001 |   10002 | 3        | 2002          | Y
            123 |     80001 |   10003 | 5        | 2003          | N
但是,查询会显示以下结果:

order_number | header_id | line_id | quantity      | hold_price_id | released_flag
         123 |     80001 |   10001 | 1             | 2001          | Y
         123 |     80001 |   10001 | 3             | 2002          | Y
         123 |     80001 |   10001 | 5             | 2003          | N
         123 |     80001 |   10002 | 1             | 2001          | Y
         123 |     80001 |   10002 | 3             | 2002          | Y
         123 |     80001 |   10002 | 5             | 2003          | N
         123 |     80001 |   10003 | 1             | 2001          | Y
         123 |     80001 |   10003 | 3             | 2002          | Y
         123 |     80001 |   10003 | 5             | 2003          | N
我的问题有问题吗?请给我一些建议


谢谢大家!

您还没有加入所有公共关键点,因此您得到的是笛卡尔结果。您需要使用头id将a连接到c,如下所示

SELECT A.order_number, A.header_id, B.line_id, B.quantity, C.hold_price_id, C.released_flag
FROM Table_A a,
     Table_B b,
     Table_C c
WHERE a.header_id = b.header_id
AND   c.line_id = b.line_id
AND   a.header_id = c.header_id
AND   a.order_number = '123';

您需要学习使用正确的显式
join
语法。一条简单的规则:在
from
子句中不要使用逗号。始终使用显式
join
s:

SELECT A.order_number, A.header_id, B.line_id, B.quantity,
       C.hold_price_id, C.released_flag
FROM Table_A a JOIN
     Table_B b
     ON a.header_id = b.header_id JOIN
     Table_C c
     ON c.header_id = b.header_id AND c.line_id = b.line_id
WHERE a.order_number = '123';

选择a.订单号、a.标题id、b.行id、b.数量、c.持有价格id、c.发布标志 从表A中选择交叉连接 表B交叉连接表C

订单号标题ID行ID数量保留价格ID发布



选择9行

请编辑您的问题并显示示例数据和所需结果。至少需要
和c.header\u id=b.header\u id