加入Sql Server 2012 - >从一个表中选择两个员工姓名,在一个存储过程中有两个不同的员工ID [英] Joins In Sql Server 2012-->select Two Employee Names from a single table with two different employee Ids in a single stored procedure

查看:127
本文介绍了加入Sql Server 2012 - >从一个表中选择两个员工姓名,在一个存储过程中有两个不同的员工ID的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

hai朋友,

我有一天尝试过以下查询,但我没有得到外出..



查询是:



 选择  distinct  L.LeadBudget,L.CompanyName,L.LeadTitle,L.Status,

E.Name as OwnerName ,E.Name as

CreatedUser

from 潜在客户L,员工E 其中 L.LeadId = ' 2'

E.EmployeeId = L.LeadOwner E.EmployeeId = L.CreatedUserId







在上面的查询中我想得到E.Name作为OwnerName和E.Name作为CreatedUser



来自单个Employee表)..其中E.EmployeeId = L.LeadOwner和



E.EmployeeId = L.CreatedUserId



如果我执行所有者姓名或创建用户我是得到数据..但我想得到两个名字..



请帮助我这方面..



提前致谢

解决方案

试试这个

 选择  distinct  L.LeadBudget,L.CompanyName,L.LeadTitle,L.Status,

E.Name as OwnerName,E.Name as

CreatedUser

来自潜在客户L join 员工E ON E.EmployeeId = L.LeadOwner E.EmployeeId = L.CreatedUserId 其中 L.LeadId = < span class =code-string>' 2'


 选择  distinct  L.LeadBudget ,L.CompanyName,L.LeadTitle,L.Status,

E. [Name] as [OwnerName],E。[Name] as

[CreatedUser]

来自潜在客户L join 员工E ON E.EmployeeId = L.LeadOwner E.EmployeeId = L.CreatedUserId 其中 L.LeadId = ' 2'


最后我得到了输出......



  SELECT  l.LeadBudget,l.CompanyName,l.LeadTitle,l.Status,leadEmp.Name  As  OwnerName,createdEmp.Name  As  CreatedUser 
FROM 潜在客户l
INNER 加入员工leadEmp ON (l.LeadOwner = leadEmp.EmployeeId)
INNER JOIN Employee createdEmp ON (l.CreatedUserId = createdEmp.EmployeeId)
WHERE l.LeadId = ' 2'


hai friends,
I tried one day about the below query but i didn''t get the out put..

Query is:

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




In The above query i want to get E.Name as OwnerName and E.Name as CreatedUser

from a single Employee tables)..where E.EmployeeId=L.LeadOwner and

E.EmployeeId=L.CreatedUserId

if i execute with out OwnerName or Created User i am getting the data..but i want to get both the names..

Please help me in this regard..

Thanks in advance

解决方案

Try this

select distinct L.LeadBudget,L.CompanyName,L.LeadTitle,L.Status,

E.Name as OwnerName,E.Name as

CreatedUser

 from Leads L join Employee E ON E.EmployeeId=L.LeadOwner and E.EmployeeId=L.CreatedUserId where L.LeadId='2'


select distinct L.LeadBudget,L.CompanyName,L.LeadTitle,L.Status,

E.[Name] as [OwnerName],E.[Name] as

[CreatedUser]

 from Leads L join Employee E ON E.EmployeeId=L.LeadOwner and E.EmployeeId=L.CreatedUserId where L.LeadId='2'


finally i got the output...

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'


这篇关于加入Sql Server 2012 - &gt;从一个表中选择两个员工姓名,在一个存储过程中有两个不同的员工ID的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆