SQL语句帮助

时间:2011-02-15 17:19:23

标签: sql

好的,所以我在这里处理4个表 -

  

项目,成员,迭代和ProjectIterationMembers。

当我创建新的项目详细信息时,我将它们写入Project表,我还指定了一个迭代(start dateend date - 一个项目至少包含一次迭代,但之后可以添加更多的迭代)。 Members表中的成员现在写入ProjectIterationMembers表,因为它们被分配给迭代。

以下SQL语句检索公司的所有项目 -

  

显示项目名称,项目经理和项目状态。

但是,我想要做的只是显示特定成员所分配到的项目。当我尝试传递MemberID时,没有检索到任何结果。

SELECT DISTINCT(Project.ProjectName), Project.ProjectID, Project.Status,   
Project.CompanyID, Project.StartDate, m.MemberID, 
m.FirstName + ' ' + m.LastName AS ProjectManager 
FROM Project, Member m, ProjectIterationMember, Iterations i WHERE 
m.CompanyRole = 'Project Manager' AND Project.CompanyID = '" + co_id + "' AND 
m.MemberID = ProjectIterationMember.MemberID AND i.ProjectIterationID = 
ProjectIterationMember.ProjectIterationID AND i.ProjectID = Project.ProjectID 
ORDER BY Project.StartDate DESC

我的表格结构如下:

Project         Iterations               ProjectIterationMember    Member 
=============== ======================== ========================= ===========
ProjectID (PK)  ProjectIterationID (PK)  ProjectIterationMemberID  MemberID
CompanyID       ProjectID                ProjectIterationID        FirstName
ProjectName     StartDate                MemberID                  LastName
StartDate       EndDate
EndDate
Description

2 个答案:

答案 0 :(得分:1)

我会试一试......

SELECT DISTINCT
  p.*,
  m.MemberID,
  m.FirstName + ' ' + m.LastName AS ProjectManager
FROM
  Project p
INNER JOIN
  Iterations i
ON
  p.ProjectID = i.ProjectID
INNER JOIN
  ProjectIterationMember pim1
ON
  i.ProjectIterationId = pim1.ProjectIterationID
INNER JOIN
  Member m
ON
  pim1.ProjectIterationMemberID = m.MemberID
AND
  m.CompanyRole = 'Project Manager'
INNER JOIN
  ProjectIterationMember pim2
ON
  i.ProjectIterationId = pim2.ProjectIterationID
AND
  pim2.ProjectIterationMemberID = ?
WHERE
  p.CompanyID = ?
ORDER BY
  p.StartDate DESC

答案 1 :(得分:0)

SELECT 
  p.ProjectName, 
  p.ProjectID, 
  p.Status,   
  p.CompanyID, 
  p.StartDate, 
  m1.MemberID, 
  m2.FirstName + ' ' + m2.LastName AS ProjectManager 
FROM 
  Project p INNER JOIN Iteration i on p.ProjectID = i.ProjectID
  INNER JOIN ProjectIterationMember pim ON i.ProjectIterationId = pim.ProjectIterationID
  INNER JOIN Member m1 ON pim.MemberID = m1.MemberID
  LEFT OUTER JOIN Memember m2 ON pim.MemberID = m2.MemberID AND m2.CompanyRole = 'Project Manager'
WHERE 
  p.CompanyID = @coId AND 
  m.MemberID = @memberId
ORDER BY 
  Project.StartDate DESC

即使分配了No PM并将memberid和companyid作为参数传递,也会给出结果。