从employees表中显示managerNames和EmployeesNames

时间:2017-10-30 20:27:51

标签: sql-server

the two tables from my DB

我需要从(EMPLOYEES)表中的[empname]返回两个值 显示

列中的

empname ---和---

他们的经理名称(empname)在另一列

部门中没有经理名称

当我编写此代码时,它返回空白值

select empname managername, deptname 
from employees , departments 
where departments.managerid=employees.empid
and
employees.empname ='arwa'

3 个答案:

答案 0 :(得分:0)

您需要使用ANSI-92样式连接。它们已存在超过25年了。此外,在您的别名中更明确,以避免事故。此查询应该可以获取您要查找的数据。

select EmployeeName = e.empname
    , DepartmaneName = d.deptname
    , Manager = man.empname
from employees e
join departments d on d.deptid = e.deptid
join employees man on man.empid = d.managerid
where e.empname = 'arwa'

答案 1 :(得分:0)

你可以试试这个:

select e1.empname, e2.empname as Manager from employees e1
join departments on e1.deptid = departments.deptid
join employees e2 on departments.managerid=e2.empid
where e1.empname = 'arwa'

答案 2 :(得分:0)

尝试从

开始
select *
from employees 
where empname ='arwa'

您会看到arwa' deptID为3.您想要将deptID 3个employeename别名添加为{{ 1}}

所以接下来加入managername表:

departments

您会看到select e.*, managerID from employees e inner join departments d on e.deptID = d.deptID where empname ='arwa' 部门arwa是7.您想要将managerID 7 managerID别名添加为{ {1}}。尝试:

employeename