我想从2个表(已加入)中提取信息。
SELECT B.INVOICE_NUMBER,
B.BILL_TO_NAME,
B.SHIP_TO_NAME //Need this some how joined, see comment on code below
FROM ADJUSTMENTS A,
INVOICE B,
CUSTOMER_LOCATION C,
CUSTOMER_NAME D
WHERE A.INVOICE_ID = B.INVOICE_ID
AND C.CUSTOMER_ID = D.CUSTOMER_ID
AND B.BILL_ID = C.ACCOUNT_NUMBER
//I want to add B.SHIP_ID = C.ACCOUNT_NUMBER as well,
//but I am unsure if it will work. Not sure how to extract information from
//two columns from the same table that is joined to one table.
我研究了子查询,但是它返回了很多行,所以它出错了。我是Oracle SQL的初学者,我尝试过研究但没有得到解决方案。任何帮助表示赞赏。
答案 0 :(得分:0)
我不能100%告诉你想要什么,但如果b.bill_id和b.ship_id有2个不同的account_number,并且每个account_number只有一个customer_location,那么你可以第二次添加customer_location别名。如果您需要第二个customer_location的customer_name记录,那么您还必须第二次加入。也许:
SELECT b.invoice_number, b.bill_to_name, b.ship_to_name
FROM adjustments a, invoice b, customer_location c,
customer_location c2, customer_name d, customer_name d2
WHERE a.invoice_id = b.invoice_id
AND c.customer_id = d.customer_id
AND c2.customer_id = d2.customer_id
AND b.bill_id = c.account_number
AND b.ship_id = c2.account_number
现在你的问题很模糊,所以很难说这是不是你想要的。如果每个account_number有多个customer_location记录,请注意每个发票发票号的多行,如果需要,可以从那里解决问题。我希望这能让你更接近你想要的东西。