简体   繁体   中英

Joining multiple tables to one table in sql

I have a rather complex (well for me) sql query happening and I am having trouble with some concepts.

I have the following sql on a webpage that i am building

SELECT 
    [dbo].[Enrolment].[_identity], [dbo].[Enrolment].CommencementDate, 
    [dbo].[Enrolment].CompletionDate, [dbo].[Enrolment].enrolmentDate, 
    [dbo].[Course].name coursename, [dbo].[Course].Identifier as QUALcode, 
    [dbo].[Person].givenName, [dbo].[Person].Surname,[dbo].[Employer].name as empname, 
    [dbo].[Employer].Address1,[dbo].[Employer].Suburb,[dbo].[Employer].Phone,
    [dbo].[Employer].PostCode,[dbo].[EnrolmentStatus].name as enrolname, 
    [dbo].[Student].identifier,[dbo].[Student].person,[dbo].[Contact].person as CONTACTid
FROM 
    (((([dbo].[Enrolment] 
LEFT JOIN 
    [dbo].[Course] ON [dbo].[Enrolment].course = [dbo].[Course].[_identity])
LEFT JOIN 
    [dbo].[Employer] ON [dbo].[Enrolment].employer = [dbo].[Employer].[_identity]) 
LEFT JOIN 
    [dbo].[EnrolmentStatus] ON [dbo].[Enrolment].status = [dbo].[EnrolmentStatus].[_identity])
LEFT JOIN 
    [dbo].[Student] ON [dbo].[Enrolment].student = [dbo].[Student].[_identity]) 
LEFT JOIN 
    [dbo].[Person] ON [dbo].[Student].person = [dbo].[Person].[_identity] 
LEFT JOIN 
    [dbo].[Contact] ON [dbo].[Employer].[_identity] = [dbo].[Contact].employer 
WHERE 
    (([dbo].[EnrolmentStatus].name) = 'training' 
    OR
    ([dbo].[EnrolmentStatus].name) = 'enrolled') 

This is working fine but what I would like to do is join to the [dbo].[Person] table again but this time joining from another table so the code I effectively need to patch into the above statement is

LEFT JOIN 
    [dbo].[Trainer] ON [dbo].[Enrolment].Trainer = [dbo].[Trainer].[_identity])

LEFT JOIN 
    [dbo].[Person] ON [dbo].[Trainer].person = [dbo].[Person].[_identity]

I then need to be able to get from the person table the name of the student and the name of the trainer, so I need 2 records from the person table for every record from the Enrolment table, the fields I need from the person table are the same for both trainer and student in that I am trying to get the given name and surname for both.

Any help or pointers would be most appreciated.

You have to just use replace your from clause with this. You have to just first use the Trainer table join, then Person table, then use the AND keyword to use multiple mapping with single table

FROM (((([dbo].[Enrolment] 
LEFT JOIN [dbo].[Course] ON [dbo].[Enrolment].course = [dbo].[Course].[_identity])
LEFT JOIN [dbo].[Employer] ON [dbo].[Enrolment].employer = [dbo].[Employer].[_identity]) 
LEFT JOIN [dbo].[EnrolmentStatus] ON [dbo].[Enrolment].status = [dbo].[EnrolmentStatus].[_identity])
LEFT JOIN [dbo].[Student] ON [dbo].[Enrolment].student = [dbo].[Student].[_identity]) 
LEFT JOIN [dbo].[Trainer] ON [dbo].[Enrolment].Trainer = [dbo].[Trainer].[_identity]) 
LEFT JOIN [dbo].[Person] ON [dbo].[Student].person = [dbo].[Person].[_identity] 
                            AND [dbo].[Trainer].person = [dbo].[Person].[_identity]
LEFT JOIN [dbo].[Contact] ON [dbo].[Employer].[_identity] = [dbo].[Contact].employer 

Use aliasing like this..

LEFT JOIN [dbo].[Trainer] ON [dbo].[Enrolment].Trainer = [dbo].[Trainer].[_identity])
LEFT JOIN [dbo].[Person] AS p ON [dbo].[Trainer].person = p.[_identity]

If I get your question right - what you are trying to do is to join the same table twice in your SQL. You have one table Person which has both student and trainer information and you want to see their details side by side in your result set. So you need to join Person once with Student and another time with Trainer

To do this - you will have to join Person table together. Give your tables an alias like the other answers have suggested. Then your FROM clause can look like this -

FROM (((([dbo].[Enrolment] 

LEFT JOIN [dbo].[Course] ON [dbo].[Enrolment].course = [dbo].[Course].[_identity])

LEFT JOIN [dbo].[Employer] ON [dbo].[Enrolment].employer = [dbo].[Employer].[_identity]) 

LEFT JOIN [dbo].[EnrolmentStatus] ON [dbo].[Enrolment].status = [dbo].[EnrolmentStatus].[_identity])

LEFT JOIN [dbo].[Student] ON [dbo].[Enrolment].student = [dbo].[Student].[_identity]) 

LEFT JOIN [dbo].[Person] P1 ON [dbo].[Student].person = P1.[_identity] 

LEFT JOIN [dbo].[Contact] ON [dbo].[Employer].[_identity] = [dbo].[Contact].employer 

LEFT JOIN [dbo].[Trainer] ON [dbo].[Enrolment].Trainer = [dbo].[Trainer].[_identity])

LEFT JOIN [dbo].[Person] P2 ON [dbo].[Trainer].person = P2.[_identity]
....
....

Here P1 and P2 are two aliases for [Person]

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM