SQL Server使用具有复合键的表加入

时间:2014-11-13 21:12:33

标签: sql-server inner-join

我需要在以下查询尝试中加入四个表:

SELECT PROJECT.PROJECT_ID
     , PROJECT_STATUS.PRO_STATUS_NAME
     , PROJECT.PROJECT_NAME
     , SKILL.SKILL_NAME
FROM PROJECT_STATUS
INNER JOIN PROJECT    ON PROJECT_STATUS.PRO_STATUS_ID = PROJECT.PRO_STATUS_ID
INNER JOIN PRO_SKILL  ON SKILL.SKILL_ID = PRO_SKILL.SKILL_ID 
                     AND PRO_SKILL.PROJECT_ID = PROJECT.PROJECT_ID;

不幸的是,PRO_SKILL表使用了复合键,因此在尝试运行查询时出现以下错误:

  

消息4104,级别16,状态1,行6多部分标识符   " SKILL.SKILL_ID"无法受约束。 Msg 4104,Level 16,State 1,Line   1多部分标识符" SKILL.SKILL_NAME"无法受约束。

有没有办法正确连接使用复合键的表?

PRO_SKILL有一个复合键: PROJECT_ID和SKILL_ID

技能只有SKILL_ID的常规PK

提前感谢您的帮助!

1 个答案:

答案 0 :(得分:0)

你错过了技能表,试试这个....

SELECT PROJECT.PROJECT_ID
     , PROJECT_STATUS.PRO_STATUS_NAME
     , PROJECT.PROJECT_NAME
     , SKILL.SKILL_NAME
FROM PROJECT_STATUS
INNER JOIN PROJECT    ON PROJECT_STATUS.PRO_STATUS_ID = PROJECT.PRO_STATUS_ID
INNER JOIN PRO_SKILL  ON PRO_SKILL.PROJECT_ID = PROJECT.PROJECT_ID
INNER JOIN SKILL      ON SKILL.SKILL_ID = PRO_SKILL.SKILL_ID