将多个表连接到sql中的一个表

时间:2014-11-27 06:02:20

标签: sql sql-server join

我有一个相当复杂的(对我而言)sql查询正在发生,我遇到了一些概念问题。

我正在构建的网页上有以下sql

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') 

这个工作正常,但我想要做的是再次加入[dbo].[Person]表,但这次从另一个表加入,所以代码我实际需要修补上面的语句是

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

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

然后我需要能够从人员表中获取学生的姓名和培训师的姓名,因此我需要来自人员表的2条记录,用于Enrolment表中的每条记录,字段我需要从人员表中同样适用于培训师和学生,因为我试图获得given namesurname两者。

任何帮助或指示都将非常感激。

3 个答案:

答案 0 :(得分:1)

你必须使用替换你的from子句。你必须先使用Trainer表连接,然后使用Person表,然后使用AND关键字使用单个表的多个映射

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 

答案 1 :(得分:0)

像这样使用别名..

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

答案 2 :(得分:0)

如果我的问题是正确的 - 您要做的是在SQL中将同一个表连接两次。您有一个表Person,其中包含学生和培训师信息,您希望在结果集中并排查看其详细信息。因此,您需要Person加入Student一次加入Trainer

要做到这一点 - 您必须将Person表连接在一起。给你的表一个别名,就像其他答案所建议的那样。然后你的FROM子句看起来像这样 -

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]
....
....

此处P1P2是[Person]

的两个别名