0

我试图从一天开始解决以下查询,但我没有得到输出..

查询是:

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 或 Created User 的情况下执行,我将获取数据..但我想同时获取这两个名称。

4

1 回答 1

0

我建议使用更现代的 JOIN 风格;而不是列出多个表并在 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' 
于 2013-02-08T12:54:20.313 回答