如何选择所有经理和相应的员工

时间:2012-07-27 17:38:00

标签: sql

ID  EMP_ID  EMP_NAE  MANAGER_ID
1     1      KARAN      2
2     2      K VEDI     3
3     3      SOMYA      2
4     4      KAVITA     3
5     5      AMAN       2

1 个答案:

答案 0 :(得分:1)

有关员工及其经理的列表,请使用:

select emp_nae as [Employee name], man.emp_nae as [Manager name]
from tablename as emp
left join tablename as man on man.emp_id = emp.manager_id

有关经理和他们的员工的列表使用此:

select emp_nae as [Employee name], man.emp_nae as [Manager name]
from tablename as man
join tablename as emp on man.emp_id = emp.manager_id