Verknüpfen von zwei Tabellen mit der Dritten Verknüpfung von Tabelle, einschließlich der null-Einträge

Ich habe eine Reihe von ähnlichen Fragen, aber habe noch stolpern/finden Sie die richtige Lösung für das problem unten.

Gegeben, die folgenden drei Tabellen:

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

Brauche ich zum erstellen einer Verknüpfung(en) zu wählen, die folgenden:

account.bill_act, account.status, account.remarks, stage.account_class

wo

profile.ecpd_profile_id = (given number)

account.profile_id und profile.reg_prof_id gleichwertig sind

stage.ecpd_profile_id und profile.ecpd_profile_id gleichwertig sind

stage.bill_acct und account.bill_acct gleichwertig sind

Ich habe versucht, die folgenden...

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 = ?

Dies funktioniert, schließt aber alle die Konto-Einträge, für die es keine Entsprechung in der Stufe.

Brauche ich, um alle Zeilen der account.bill_acct=stage.bill_acct, das hinzufügen einer zusätzlichen Spalte für die stage.account_class wo es vorhanden ist, oder null sonst.

Mehrere joins werfen immer mich.

Gedanken?

Ich glaube, dass alles, was Sie suchen, ist ein LEFT JOIN auf der Bühne anstatt des normalen (INNEREN) JOIN

InformationsquelleAutor David Nedrow | 2012-09-08

Schreibe einen Kommentar