我正在尝试将多个表连接在一起以构建报告。该报告列出了一个课程,对其进行的修订,以及谁请求,制作和批准了修订。
根据要求,批准,价值是员工编号。我正在尝试使用Employee表加入上面的内部连接表,这样我就可以列出那些请求,制作和批准修订的名称(不仅仅是员工编号)。
这就是我所知道的完全错误。
SELECT *
FROM Courses
INNER JOIN CourseRevisions ON CourseRevisions.PELID = Courses.PELID
INNER JOIN CourseGroups ON CourseGroups.CourseGroupID = Courses.CourseGroupID
INNER JOIN [dbo].[OPG_Employees] ON OPG_Employees.EmployeeID = CourseRevisions.UpdatedBy
AND OPG_Employees.EmployeeID = CourseRevisions.ApprovedBy
AND OPG_Employees.EmployeeID = CourseRevisions.RequestedBy
这只会返回一个结果,恰好具有为所有3个列出的相同员工ID(请求,批准和更新)
我如何得到它以便我可以获得每个员工的表格结果?
答案 0 :(得分:1)
您必须为每个字段加入OPG_Employees表一次,即上例中的3次。一个INNER加入它为UpdatedBy,一个INNER JOIN为ApprovedBy,一个INNER JOIN for RequestedBy。
像这样:
SELECT *
FROM Courses
INNER JOIN CourseRevisions ON CourseRevisions.PELID = Courses.PELID
INNER JOIN CourseGroups ON CourseGroups.CourseGroupID = Courses.CourseGroupID
INNER JOIN [dbo].[OPG_Employees] empUpdatedBy ON empUpdatedBy.EmployeeID = CourseRevisions.UpdatedBy
INNER JOIN [dbo].[OPG_Employees] empApprovedBy ON empApprovedBy.EmployeeID = CourseRevisions.ApprovedBy
INNER JOIN [dbo].[OPG_Employees] empRequestedBy ON empRequestedBy.EmployeeID = CourseRevisions.RequestedBy
答案 1 :(得分:1)
您需要为每个被引用的员工单独加入:
SELECT *
FROM Courses INNER JOIN
CourseRevisions
ON CourseRevisions.PELID = Courses.PELID INNER JOIN
CourseGroups
ON CourseGroups.CourseGroupID = Courses.CourseGroupID INNER JOIN
[dbo].[OPG_Employees] UpdateEmp
ON UpdateEmp.EmployeeID = CourseRevisions.UpdatedBy INNER JOIN
[dbo].[OPG_Employees] ApprovedEmp
on OPG_ApprovedEmp.EmployeeID = CourseRevisions.ApprovedBy INNER JOIN
[dbo].[OPG_Employees] RequestedEmp
on RequestedEmp.EmployeeID = CourseRevisions.RequestedBy
您的原始配方要求所有三个ID完全相同。