简体   繁体   中英

Joining 3 tables Oracle SQL

I have 3 tables listing below:

Table_A:

order_number | header_id
        123  | 80001

Table_B

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

Table_C

   header_id | line_id | hold_price_id | released_flag
       80001 |   10001 | 2001          | Y
       80001 |   10002 | 2002          | Y
       80001 |   10003 | 2003          | N

I wrote a query as shown below:

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';

My desire output is as shown below:

   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

However the query show me the below result:

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

Is it something wrong on my query? Please advice.

Thank you!

You need to learn to use proper explicit join syntax. A simple rule: never use commas in the from clause. Always use explicit 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';

You haven't joined all of the common keys, so you are getting Cartesian results. You needs to join a to c with header id, like so

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';

SELECT a.order_number, a.header_id, b.line_id, b.quantity ,c.hold_price_id,c.released_flag FROM Table_A a cross JOIN Table_B b cross join table_C c;

ORDER_NUMBER HEADER_ID LINE_ID QUANTITY HOLD_PRICE_ID RELEASED_F


     123      80001      10001          1          2001 Y         
     123      80001      10002          3          2001 Y         
     123      80001      10003          5          2001 Y         
     123      80001      10001          1          2002 Y         
     123      80001      10002          3          2002 Y         
     123      80001      10003          5          2002 Y         
     123      80001      10001          1          2003 N         
     123      80001      10002          3          2003 N         
     123      80001      10003          5          2003 N         

9 rows selected.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM