从连接表中获取数据,使用SQL访问子类别

时间:2015-07-17 23:43:31

标签: sql ms-access

我使用Access数据库创建了这种关系,但是当我创建一个查询来返回值时,我没有获得所有数据。 enter image description here 如何从tubetmc获取ProjectTypeProjectId表中的所有数据?

所以我应该得到类似这样的查询,但访问生成的查询不起作用

    SELECT Project.ProjectName, 
           ProjectSubTypesId.ProjectSubTypesId, 
           Tube.TubeName, Tube.Duration, 
           Tmc.TmcName
   FROM Tmc 
INNER JOIN 
(Tube INNER JOIN 
  (Project INNER JOIN ProjectSubTypesId ON Project.ProjectId = ProjectSubTypesId.ProjectId) 
ON Tube.TubeId = ProjectSubTypesId.TubeId) ON Tmc.TmcId = ProjectSubTypesId.TmcId;

2 个答案:

答案 0 :(得分:1)

试试这个:

SELECT Project.ProjectName, 
       ProjectSubTypesId.ProjectSubTypesId
    FROM (
       (ProjectSubTypes
           INNER JOIN Project USING (ProjectId)
       )
           INNER JOIN Tube USING(TubeId)
       )
           INNER JOIN Tmc USING(TmcId)
GROUP BY ProjectSubTypes.ProjectId, ProjectSubTypes.TubeId, ProjectSubTypes.TmcId
ORDER BY ProjectSubTypes.ProjectId;

答案 1 :(得分:0)

像这样的工作吗?

SELECT Project.ProjectName, 
       ProjectSubTypesId.ProjectSubTypesId, 
       Tube.TubeName, Tube.Duration, 
       Tmc.TmcName
FROM ((ProjectSubTypes
           INNER JOIN Project ON Project.ProjectId = ProjectSubTypes.ProjectId)
       INNER JOIN Tube ON Tube.TubeId = ProjectSubTypes.TubeId)
INNER JOIN Tmc ON Tmc.TmcId = ProjectSubTypes.TmcId
ORDER BY ProjectSubTypes.ProjectId;

这应该为您提供所有子类型的列表及其相应的Tube,Tmc和Project数据。

如果可能有多个ProjectSubTypes行具有相同的TubeIdTmcId,请考虑在ORDER BY行之前添加此行:

GROUP BY ProjectSubTypes.ProjectId, ProjectSubTypes.TubeId, ProjectSubTypes.TmcId