如何在sql server中的单列上多次连接单个表

时间:2013-05-29 08:20:42

标签: sql sql-server

我有以下表格:

1. tbPatientEpisode (
    EpisodeIDP
    EpisodeNumber
    DoctorIDF
    PatientIDF
)

2. tbPatient (
    PatientIDP
    CitizenIDF
)

3. tbDoctor(
    DoctorIDP
    CitizenIDF
)

4. tbCitizen(
    CitizenIDP
    CitizenName
    City
    ContactNo
)

我想查询tbPatientEpisode表,连续获取DoctorName和PatientName,但PatientIDF和DoctorIDF指向tbCitizen。所以请帮助加入tbCitizen表两次。

1 个答案:

答案 0 :(得分:1)

试试这个适合你,

    Select pe.*, c1.CitizenName as PatientName, c2.CitizenName as DoctorName 
    from tbPatientEpisode pe
    join tbPatient p on pe.PatientIDF = p.PatientIDP
    join tbDoctor d on pe.DoctorIDF = d.DoctorIDP
    join tbCitizen c1 on p.CitizenIDF = c1.CitizenIDP
    join tbCitizen c2 on d.CitizenIDF = c2.CitizenIDP