SQL自连接问题

时间:2018-05-15 22:36:38

标签: mysql sql oracle

我有一个包含以下列的表:

ID employeeID employee status  mgrID
1  amg        active           xyz
3  ccc        inactive         pqr
4  kkk        active           LLL
5  ppp        inactive         NNN
6  xyz        active           RRR
7  GGG        inactive         ccc 

我需要将数据转换成以下格式:

EmployeeID, employee status, mgr ID, mgr Status where Employee ID in (amg,kkk,xyz,GGG)

1 个答案:

答案 0 :(得分:0)

这有帮助吗?

http://sqlfiddle.com/#!4/6932c/8/0

select e."employeeID", e."employee status", m."employeeID" as mgr_id, m."employee status" as mgr_status
from emp e, emp m
where m."employeeID" = e."mgrID"
and e."employeeID" in ('amg','kkk','xyz','GGG');
  

输出

employeeID  employee status MGR_ID  MGR_STATUS
GGG         inactive        ccc     inactive
amg         active          xyz     active