加入两个表,然后加入第三个表

时间:2012-10-01 17:44:01

标签: sql database

我有一个SQL查询,我希望将两个表连接到gether,然后使用该连接表加入第三个表。我试着谷歌搜索如何做到这一点,但这是一个尴尬的措辞,没有发现任何有用的东西。这是我试图使用的代码:

SELECT t.id AS ID
, a.id AS ActivityID
, t.ProjectType AS ProjectType
, t.Tier1Mission AS Mission
, m.id ASMissionID
, m.name AS MissionName
, t.Tier2Activity AS Activity
, a.name AS ActivityName
, t.Tier3Project AS Project
FROM tActivity a
 INNER JOIN 
 (SELECT id, name FROM tMission) m
 ON tActivity.missionId = MissionID
 LEFT OUTER JOIN
 (SELECT *
  FROM tTaxonomy
  WHERE Tier1Mission = m.name AND Tier2Activity = a.name)

编辑: 我遇到的主要问题是tActivity表的条目具有相同的“名称”,但引用了不同的MissionId。加入所有表格很简单,但需要有正确的MissionID,关系保存在第三个表'tTaxonomy'中。

3 个答案:

答案 0 :(得分:5)

你几乎拥有它,但需要一个别名和一个ON子句,用于第二次加入WHERE子句的insetad。另外,在您的第一个ON子句中,使用表别名a而不是原始名称。

SELECT t.id AS ID
, a.id AS ActivityID
, t.ProjectType AS ProjectType
, t.Tier1Mission AS Mission
, m.id ASMissionID
, m.name AS MissionName
, t.Tier2Activity AS Activity
, a.name AS ActivityName
, t.Tier3Project AS Project
FROM 
  tActivity a
  INNER JOIN 
   (SELECT id, name FROM tMission) m
     ON a.missionId = m.id
  LEFT OUTER JOIN
   (SELECT *
     FROM tTaxonomy
   ) t ON t.Tier1Mission = m.name AND t.Tier2Activity = a.name

但是,看看这个,我发现没有什么需要使用连接的子查询。子查询中没有聚合或限制以使它们成为必需。您可以使用普通表连接:

SELECT t.id AS ID
, a.id AS ActivityID
, t.ProjectType AS ProjectType
, t.Tier1Mission AS Mission
, m.id ASMissionID
, m.name AS MissionName
, t.Tier2Activity AS Activity
, a.name AS ActivityName
, t.Tier3Project AS Project
FROM
  tActivity a
  INNER JOIN tMission m ON a.missionId = m.id
  LEFT JOIN tTaxonomy t ON t.Tier1Mission = m.name AND t.Tier2Activity = a.name

答案 1 :(得分:2)

select t.id as ID,
    a.id as ActivityID,
    t.ProjectType as ProjectType,
    t.Tier1Mission as Mission,
    m.id ASMissionID,
    m.name as MissionName,
    t.Tier2Activity as Activity,
    a.name as ActivityName,
    t.Tier3Project as Project
from tActivity a
inner join tMission m on a.missionId = m.MissionID
left outer join tTaxonomy t on t.Tier1Mission = m.name and t.Tier2Activity = a.name

答案 2 :(得分:1)

看起来你很近,你在JOIN的错误位置使用JOIN语法:

SELECT t.id AS ID
    , a.id AS ActivityID
    , t.ProjectType AS ProjectType
    , t.Tier1Mission AS Mission
    , m.id ASMissionID
    , m.name AS MissionName
    , t.Tier2Activity AS Activity
    , a.name AS ActivityName
    , t.Tier3Project AS Project
FROM tActivity a
INNER JOIN 
(
    SELECT id, name 
    FROM tMission
) m
 ON a.missionId = MissionID
LEFT OUTER JOIN
(
    SELECT *
    FROM tTaxonomy
) t
    on m.name = t.Tier1Mission
    AND a.name = t.Tier2Activity

甚至:

SELECT t.id AS ID
    , a.id AS ActivityID
    , t.ProjectType AS ProjectType
    , t.Tier1Mission AS Mission
    , m.id ASMissionID
    , m.name AS MissionName
    , t.Tier2Activity AS Activity
    , a.name AS ActivityName
    , t.Tier3Project AS Project
FROM tActivity a
INNER JOIN tMission m
    ON a.missionId = MissionID
LEFT OUTER JOIN tTaxonomy t
    on m.name = t.Tier1Mission
    AND a.name = t.Tier2Activity