使用第三个作为链接表连接两个表,包括空条目

Dav*_*row 5 sql oracle join ansi-sql

我已经看了很多类似的问题,但还没有发现/找到下面问题的正确解决方案.

给出以下三个表:

account
    profile_id number (nullable)
    bill_acct varchar
    status varchar (nullable)
    remarks varchar (nullable)


stage
    ecpd_profile_id number (nullable)
    bill_account varchar (nullable)
    account_class varchar (nullable)

profile
    ecpd_profile_id number
    reg_prof_id number
Run Code Online (Sandbox Code Playgroud)

我需要创建一个连接来选择以下内容:

account.bill_act, account.status, account.remarks, stage.account_class
Run Code Online (Sandbox Code Playgroud)

哪里

profile.ecpd_profile_id = (given number)
Run Code Online (Sandbox Code Playgroud)

account.profile_id并且profile.reg_prof_id是等价的

stage.ecpd_profile_id并且profile.ecpd_profile_id是等价的

stage.bill_acct并且account.bill_acct是等价的

我试过以下......

select
    account.bill_acct,
    account.status,
    account.remarks,
    stage.account_class
from
    registration_account account
        join registration_profile profile
            on account.profile_id = profile.reg_prof_id
        join acct_stg stage
            on stage.ecpd_profile_id = profile.ecpd_profile_id
                and stage.bill_acct = account.bill_acct
where
    profile.ecpd_profile_id = ?
Run Code Online (Sandbox Code Playgroud)

这有效,但不包括阶段中没有匹配的所有帐户条目.

我需要拥有所有行account.bill_acct=stage.bill_acct,stage.account_class为其存在的位置追加一列,否则为null.

多次加入总是让我失望.

思考?

Rob*_*ert 6

尝试左连接:

select
    account.bill_acct,
    account.status,
    account.remarks,
    stage.account_class
from
    registration_account account
    left join registration_profile profile
            on account.profile_id = profile.reg_prof_id
    left join acct_stg stage
            on stage.ecpd_profile_id = profile.ecpd_profile_id
                and stage.bill_acct = account.bill_acct
where
    profile.ecpd_profile_id = ?
Run Code Online (Sandbox Code Playgroud)