关联名称被多次指定

时间:2018-06-22 13:59:31

标签: sql-server

我正在尝试加入不同的表,而该表恰好在多个表中具有同一表的外键。我收到此错误

Msg 1011, Level 16, State 1, Line 1
The correlation name 'Amount' is specified multiple times in a FROM clause.

这就是我的查询内容

SELECT [Allowance].[FullName] [Allowance]

FROM [AppCNF].[tbl_AllowanceOrBenefitType] [Allowance]

LEFT JOIN [HRM].[tbl_EmployeeAllowanceOrBenefit] [Amount] ON [Amount].[AllowanceOrBenefitTypeCode] = [Allowance].[ID]
LEFT JOIN [HRM].[tbl_PayGradeEarningsAllocation] [Earning] ON [Earning].[AllowanceOrBenefitCode] = [Allowance].[ID] 
LEFT JOIN [HRM].[tbl_PayGrade] [Grade] ON [Earning].[PayGradeCode] = [Grade].[ID]
LEFT JOIN [HRM].[tbl_EmployeeInfo] [Info] ON [Info].[PayGradeCode] = [Grade].[ID] 
LEFT JOIN [HRM].[tbl_EmployeeAllowanceOrBenefit] [Amount] ON [Info].[ID] = [Amount].[EmpCode]

有人可以在这里以正确的方式指出我吗?

2 个答案:

答案 0 :(得分:0)

这更容易显示在答案中。注意箭头:

SELECT [Allowance].[FullName] [Allowance]

FROM [AppCNF].[tbl_AllowanceOrBenefitType] [Allowance]

LEFT JOIN [HRM].[tbl_EmployeeAllowanceOrBenefit] [Amount] <---- ON [Amount].[AllowanceOrBenefitTypeCode] = [Allowance].[ID]
LEFT JOIN [HRM].[tbl_PayGradeEarningsAllocation] [Earning] ON [Earning].[AllowanceOrBenefitCode] = [Allowance].[ID] 
LEFT JOIN [HRM].[tbl_PayGrade] [Grade] ON [Earning].[PayGradeCode] = [Grade].[ID]
LEFT JOIN [HRM].[tbl_EmployeeInfo] [Info] ON [Info].[PayGradeCode] = [Grade].[ID] 
LEFT JOIN [HRM].[tbl_EmployeeAllowanceOrBenefit] [Amount] <----- ON [Info].[ID] = [Amount].[EmpCode]

您不能有两个具有相同别名的对象,因此您需要使用不同的对象。您可以,例如:

SELECT [Allowance].[FullName] [Allowance]
FROM [AppCNF].[tbl_AllowanceOrBenefitType] [Allowance]
     LEFT JOIN [HRM].[tbl_EmployeeAllowanceOrBenefit] [BenefitAmount] ON [BenefitAmount].[AllowanceOrBenefitTypeCode] = [Allowance].[ID]
     LEFT JOIN [HRM].[tbl_PayGradeEarningsAllocation] [Earning] ON [Earning].[AllowanceOrBenefitCode] = [Allowance].[ID]
     LEFT JOIN [HRM].[tbl_PayGrade] [Grade] ON [Earning].[PayGradeCode] = [Grade].[ID]
     LEFT JOIN [HRM].[tbl_EmployeeInfo] [Info] ON [Info].[PayGradeCode] = [Grade].[ID]
     LEFT JOIN [HRM].[tbl_EmployeeAllowanceOrBenefit] [InfoAmount] ON [Info].[ID] = [InfoAmount].[EmpCode];

编辑:就个人而言,我更喜欢实际使用更小的别名。例如,对于tbl_EmployeeAllowanceOrBenefit,我将使用EAB( E mployee A llowanceOr B enefit)。这会将您的查询变成:

SELECT ABT.FullName Allowance
FROM AppCNF.tbl_AllowanceOrBenefitType ABT
     LEFT JOIN HRM.tbl_EmployeeAllowanceOrBenefit EAB ON EAB.AllowanceOrBenefitTypeCode = ABT.ID
     LEFT JOIN HRM.tbl_PayGradeEarningsAllocation PGEA ON PGEA.AllowanceOrBenefitCode = ABT.ID
     LEFT JOIN HRM.tbl_PayGrade PG ON PGEA.PayGradeCode = PG.ID
     LEFT JOIN HRM.tbl_EmployeeInfo EI ON EI.PayGradeCode = PG.ID
     LEFT JOIN HRM.tbl_EmployeeAllowanceOrBenefit EABei ON EI.ID = EABei.EmpCode;

这使它的肿程度降低了(尽管对于那些真正大的列名而言,它没有那么多)。

答案 1 :(得分:0)

我想您要对现有联接应用附加条件,而不是第二次联接到员工津贴表中。像这样:

SELECT [Allowance].[FullName] [Allowance]

FROM [AppCNF].[tbl_AllowanceOrBenefitType] [Allowance]

LEFT JOIN [HRM].[tbl_EmployeeAllowanceOrBenefit] [Amount]
    ON [Amount].[AllowanceOrBenefitTypeCode] = [Allowance].[ID]
LEFT JOIN [HRM].[tbl_PayGradeEarningsAllocation] [Earning]
    ON [Earning].[AllowanceOrBenefitCode] = [Allowance].[ID] 
LEFT JOIN [HRM].[tbl_PayGrade] [Grade]
    ON [Earning].[PayGradeCode] = [Grade].[ID]
LEFT JOIN [HRM].[tbl_EmployeeInfo] [Info]
    ON [Info].[PayGradeCode] = [Grade].[ID] 
    AND [Info].[ID] = [Amount].[EmpCode] --<--This condition applies to the Info join

请记住,联接通常在表之间不是,而在先前联接的结果和新表 1 之间。 ON子句可以访问形成先前结果的任何表别名。


1 或者甚至是两个之前的联接结果之间的联接,当您考虑可以以许多不同的方式指定联接顺序并且不一定遵循查询的“读取”顺序时。