Oracle SELECT子查询

时间:2018-05-10 07:31:07

标签: oracle

我有两张桌子。一个用于交易清单,一个用于参考

Transaction:


ID | Agency ID | Advertiser ID | Code
1  | 123       | 440           | samplecode

Reference:

ID  | LongName    | Type
123 | Agency1     | Agency
440 | Advertiser1 | Advertiser

如何在Oracle中编写SQL子查询,以便我可以在事务表中的SELECT语句中包含LongName和Type,这样它将如下所示:

ID | Agency ID | LongName | Type   | Advertiser ID | LongName    | Type       | Code
1  | 123       | Agency1  | Agency | 440           | Advertiser1 | Advertiser | samplecode

1 个答案:

答案 0 :(得分:1)

您可以将Transaction加入Reference两次:

SELECT
    t.ID,
    t."Agency ID",
    r1.LongName AS ln1,
    r1.Type AS type1,
    t."Advertiser ID",
    r2.LongName AS ln2,
    r2.Type AS type2,
    t.Code
FROM Transaction t
INNER JOIN Reference r1
    ON t."Agency ID" = r1.ID
INNER JOIN Reference r2
    ON t."Advertiser ID" = r2.ID