我看过很多类似的问题,但还没有找到下面问题的正确解决方案。
给出以下三张表:
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
我需要创建一个或多个联接来选择以下内容:
account.bill_act, account.status, account.remarks, stage.account_class
在哪里
profile.ecpd_profile_id = (given number)
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 = ?
此方法可行,但排除了在stage表中没有匹配项的所有账户条目。
我需要所有满足account.bill_acct=stage.bill_acct
条件的行,并在存在stage.account_class
时添加一个额外的列,否则为null。
多个联接总是让我感到困惑。
有什么想法吗?