LEFT OUTER JOIN 3表

时间:2012-10-02 15:44:36

标签: sql sql-server sql-server-2012 outer-join

我正在更新使用旧结构进行外连接的查询(= *和* =)。我有3个表,我需要包含在外连接中。

原始查询是:

SELECT  s.SkillID ,
        NULL AS Signature ,
        NULL AS DPL ,
        CASE WHEN ISNULL(ds.DPL, dg.DPL) IS NULL
             THEN p.ScaleTo - p.ScaleFrom + 1
             ELSE ISNULL(ds.DPL, dg.DPL)
        END AS DefaultDPL
FROM    tbJobs j ,
        tbSkills s 
        INNER JOIN tbSkillGroups sg ON s.SkillGroupID = sg.SkillGroupID ,
        tbPerfScales p ,
        tbDPLs ds ,
        tbDPLs dg
WHERE   j.JobID = 866
        AND ( ds.LevelID=*j.LevelID
              AND ds.IDType = 1
              AND ds.GroupOrSkillID=*s.SkillID
            )
        AND ( dg.LevelID=*j.LevelID
              AND dg.IDType = 0
              AND dg.GroupOrSkillID=*sg.SkillGroupID
            )
        AND ( ( s.PerfScaleID IS NOT NULL
                AND p.PerfScaleID = s.PerfScaleID
              )
              OR ( s.PerfScaleID IS NULL
                   AND p.PerfScaleID = sg.PerfScaleID
                 )
            )

我在做:

SELECT  s.SkillID ,
        NULL AS Signature ,
        NULL AS DPL ,
        CASE WHEN ISNULL(ds.DPL, dg.DPL) IS NULL
             THEN p.ScaleTo - p.ScaleFrom + 1
             ELSE ISNULL(ds.DPL, dg.DPL)
        END AS DefaultDPL
FROM    tbPerfScales p ,
        tbSkills s
        INNER JOIN tbSkillGroups sg ON s.SkillGroupID = sg.SkillGroupID ,
        tbJobs j
        LEFT OUTER JOIN tbDPLs ds ON j.LevelID = ds.LevelID
                                     AND s.SkillID = ds.GroupOrSkillID
        LEFT OUTER JOIN tbDPLs dg ON j.LevelID = dg.LevelID
                                     AND sg.SkillGroupID = dg.GroupOrSkillID
WHERE   j.JobID = 866
        AND ds.IDType = 1
        AND dg.IDType = 0
        AND ( ( s.PerfScaleID IS NOT NULL
                AND p.PerfScaleID = s.PerfScaleID
              )
              OR ( s.PerfScaleID IS NULL
                   AND p.PerfScaleID = sg.PerfScaleID
                 )
            )

出于某种原因,我收到错误:

  

无法绑定多部分标识符“s.SkillID”。

我知道就是这部分:

    tbJobs j
    LEFT OUTER JOIN tbDPLs ds ON j.LevelID = ds.LevelID
                                 AND s.SkillID = ds.GroupOrSkillID

我不确定我做错了什么。

感谢任何帮助。 何

2 个答案:

答案 0 :(得分:6)

声明后有一个逗号

INNER JOIN tbSkillGroups sg ON s.SkillGroupID = sg.SkillGroupID ,

这是因为你之后有tbJobs,它应该在其他表中。我建议使用CROSS JOIN,而不是在FROM子句中只有多个表,因为它会更清楚。

以下是您的查询的基本重写应该有效,但您应该能够轻松摆脱大多数交叉连接。

SELECT  s.SkillID ,
        NULL AS Signature ,
        NULL AS DPL ,
        CASE WHEN ISNULL(ds.DPL, dg.DPL) IS NULL
             THEN p.ScaleTo - p.ScaleFrom + 1
             ELSE ISNULL(ds.DPL, dg.DPL)
        END AS DefaultDPL
FROM    tbPerfScales p 
        CROSS JOIN tbSkills s
        CROSS JOIN tbJobs j
        INNER JOIN tbSkillGroups sg ON s.SkillGroupID = sg.SkillGroupID
        LEFT OUTER JOIN tbDPLs ds ON j.LevelID = ds.LevelID
                                     AND s.SkillID = ds.GroupOrSkillID
        LEFT OUTER JOIN tbDPLs dg ON j.LevelID = dg.LevelID
                                     AND sg.SkillGroupID = dg.GroupOrSkillID
WHERE   j.JobID = 866
        AND ds.IDType = 1
        AND dg.IDType = 0
        AND ( ( s.PerfScaleID IS NOT NULL
                AND p.PerfScaleID = s.PerfScaleID
              )
              OR ( s.PerfScaleID IS NULL
                   AND p.PerfScaleID = sg.PerfScaleID
                 )
            )

答案 1 :(得分:3)

您不能混用不同的SQL语法。如果使用ANSI-92,则需要更正所有连接,而不仅仅是外连接。一致性是关键。

逗号现在是不正确的语法。你需要为每个表添加一个连接,或者根据Darren的答案将它们拉出来交叉应用。 这就是问题所在:

FROM    tbPerfScales p ,
        tbSkills s
        INNER JOIN tbSkillGroups sg ON s.SkillGroupID = sg.SkillGroupID ,
        tbJobs j
        LEFT OUTER JOIN tbDPLs ds ON j.LevelID = ds.LevelID
                                     AND s.SkillID = ds.GroupOrSkillID
        LEFT OUTER JOIN tbDPLs dg ON j.LevelID = dg.LevelID
                                     AND sg.SkillGroupID = dg.GroupOrSkillID

它需要遵循:

  FROM    tbPerfScales p 
     INNER JOIN tbSkills s ON  p.<field> = s.<field>
        INNER JOIN tbSkillGroups sg ON s.SkillGroupID = sg.SkillGroupID
        INNER JOIN tbJobs j on j.<field> = <table p? sg? not sure>.<field>
        LEFT OUTER JOIN tbDPLs ds ON j.LevelID = ds.LevelID
                                     AND s.SkillID = ds.GroupOrSkillID
        LEFT OUTER JOIN tbDPLs dg ON j.LevelID = dg.LevelID
                                     AND sg.SkillGroupID = dg.GroupOrSkillID