Oracle SQL - Select from two tables
I have two tables which are exactly the same layout, but have different data (one is current, one is history).
How do I pull information from both tables?
PSUDO SQL:
SELECT
T.TRANS_QUAN,
P.PONO, D.ID
FROM
TRANSLOG T, (select * from PO, PO_HIST) P, (SELECT * FROM PO_DETAIL, PO_DETAIL_HIST) D
WHERE
D.PO_ID = P.ID
AND T开发者_开发问答.SOURCE_ID = D.ID
When I try to actually run that I get "column ambiguously defined"
You can UNION clause.
Try:
SELECT
T.TRANS_QUAN,
P.PONO, D.ID
FROM
TRANSLOG T,
(
SELECT * FROM PO
UNION
SELECT * FROM PO_HIST
) P,
(
SELECT * FROM PO_DETAIL
UNION
SELECT * FROM PO_DETAIL_HIST
) D
WHERE
D.PO_ID = P.ID
AND T.SOURCE_ID = D.ID
(select * from PO, PO_HIST) P
This subquery might make a cross join of tables that share a common column name.
Maybe you want:
Select t.trang_quan
, p.pono
, d.id
from translog t
inner join po_detail d
on d.id = t.source_id
cross join po_detail_hist
inner join po p
on p.id = d.po_id
cross join po_hist p2
Are you sure you want cross joins?
精彩评论