如何在没有重复的情况下选择所有列和其中一列

时间:2017-10-30 23:06:42

标签: sql-server

enter image description here

enter image description here

当我这样做时

select * , empname as managerName

我希望所有数据只有managerName

2 个答案:

答案 0 :(得分:0)

如果要列出员工和每个员工的部门经理的姓名,您需要两次加入Employee表。一种方法是:

Select Employees.*
   , Departments.*
   , Managers.EmployeeName as ManagerName
From Employees
Inner Join Departments on departments.DeptID = employees.DeptID
Inner Join Employees managers on managers.EmpId = departments.ManagerID

答案 1 :(得分:0)

除了你需要更多地解决问题之外......

如果您要做的就是找到经理......您只需要使用经理ID将部门加入员工......就像这样

select 
e.empid
,e.empname
,e.jobtitle
,e.salary
,d.deptname
,m.empname as manager_name

from employee e

    inner join departments d
        on d.deptid = e.deptid

    left join employee m
        on m.empid = d.managerid