我有一个tblEmployeeProfile&一个tblPersonnel。 tblPersonnel是一个HR表,由公司的所有员工组成; tblEmployeeProfile包含有关员工职位的详细信息。
tblPersonnel.PersonnelID
tblPersonnel.FirstName
tblPersonnel.MiddleName
tblPersonnel.LastName
tblPersonnel.PhoneNumber
tblPersonnel.Email
tblEmployeeProfile.EmployeeID
tblEmployeeProfile.ManagerID
tblEmployeeProfile.DepartmentID
tblEmployeeProfile.JobCategoryID
tblEmployeeProfile.SalaryID
我想返回包含以下字段的记录:
EmployeeID, FirstName, MiddleName, LastName, Email, ManagerFullName
where EmployeeID = @EmployeeID
*tblEmployeeProfile.ManagerID = tblPersonnel.PersonnelID*
我似乎无法使查询正确获取ManagerFullName
答案 0 :(得分:2)
您可能需要尝试以下操作:
SELECT
e1.EmployeeID,
e1.FirstName,
e1.MiddleName,
e1.LastName,
e1.Email,
e2.FirstName + ' ' + e2.LastName AS ManagerFullName
FROM
tblPersonnel e1
INNER JOIN
tblEmployeeProfile ep ON (ep.EmployeeID = e1.PersonnelID)
INNER JOIN
tblPersonnel e2 ON (e2.PersonnelID = ep.ManagerID)
WHERE
e1.EmployeeID = @EmployeeID
答案 1 :(得分:2)
不确定Tsql是否相同,但是你需要两个连接语句。第一次加入是员工表中的员工个人资料表。其次是将人员表加入到配置文件表中以获取管理员名称。可能看起来像这样
FROM personnel p
JOIN employeeprofile prof
ON prof.employeeID = p.personnelID
LEFT OUTER JOIN personnel man
ON man.personnelID = prof.managerID
我在经理人身上做左外连接的唯一原因是奇怪的是,用户可能没有分配经理。
答案 2 :(得分:1)
SELECT
employee.PersonnelID, employee.FirstName, employee.MiddleName, employee.LastName, employee.Email, manager.FirstName + ' ' + manager.Surname
FROM
tblPersonnel AS employee
INNER JOIN tblEmployeeProfile ON employee.PersonnelID = tblEmployeeProfile.EmployeeID
INNER JOIN tblPersonnel AS manager ON tblEmployeeProfile.ManagerID = manager.PersonnelID
WHERE
employee.PersonnelID = @EmployeeID
答案 3 :(得分:0)
试试这个:
SELECT
p1.PersonnelID,
p1.FirstName,
p1.MiddleName,
p1.LastName,
p1.Email,
p2.FirstName + ' ' + p2.MiddleName + ' ' + p2.LastName as ManagerFullName
FROM
tblEmployeeProfile e,
tblPersonnel p1,
tblPersonnel p2
WHERE
e.EmployeeId = p1.PersonnelId AND
e.ManagerId = p2.PersonnelId AND
e.EmployeeId = @EmployeeId
答案 4 :(得分:0)
select p.EmployeeID
, E.FirstName
, E.MiddleName
, E.LastName
, E.Email
, M.FirstName
+ ' '
+ M.MiddleName
+ ' '
+ M.LastNameas as ManagerFullName
from tblPersonnel p
join tblEmployeeProfile as E on p.PersonnelID=E.EmplyeeID
join tblEmployeeProfile as M on M.PersonnelID=E.ManagerID
where E.EmplyeeID=@EmployeeID
当然假设:
答案 5 :(得分:0)
你想做这样的事吗?
Select
EmployeeID
,empTable.FirstName
,empTable.MiddleName
,empTable.LastName
,empTable.Email
,mgrTable.FirstName + ' '
+ mgrTable.MiddleName + ' '
+ mgrTable.LastName
as ManagerFullName
from
tblEmployeeProfile
inner join
tblPersonnel as empTable
on tblEmployeeProfile.EmployeeID = empTable.PersonnelID
inner join
tblPersonnel as mgrTable
on tblEmployeeProfile.ManagerID = mgrTable.PersonnelID
where EmployeeID = @EmployeeID