从单个表中选择两个员工姓名,在单个存储过程中具有两个不同的员工ID

时间:2013-02-08 12:40:33

标签: sql

我试图在一天之内解决以下问题,但我没有得到出局......

查询是:

select distinct L.LeadBudget,L.CompanyName,L.LeadTitle,L.Status,
E.Name as OwnerName,E.Name as 
CreatedUser
 from Leads L,Employee E where L.LeadId='2' 
 and E.EmployeeId=L.LeadOwner and E.EmployeeId=L.CreatedUserId

在上面的查询中我想得到

E.Name as OwnerName and E.Name as CreatedUser 
from a single Employee tables)..where E.EmployeeId=L.LeadOwner and 
E.EmployeeId=L.CreatedUserId

如果我执行没有OwnerName或创建用户我正在获取数据..但我想得到这两个名称。

1 个答案:

答案 0 :(得分:0)

我建议使用更现代的JOIN风格;而不是列出多个talbes并在WHERE中执行所有操作。 基本上要实现您想要做的事情,您需要为两位员工执行JOIN。

SELECT l.LeadBudget, l.CompanyName, l.LeadTitle, l.Status, leadEmp.Name As OwnerName, createdEmp.Name As CreatedUser
FROM Leads l
     INNER JOIN Employee leadEmp ON(l.LeadOwner = leadEmp.EmployeeId)
     INNER JOIN Employee createdEmp ON(l.CreatedUserId= createdEmp.EmployeeId)
WHERE l.LeadId='2'