在MySQL中加入同一个表? [英] join with same table in mysql?
本文介绍了在MySQL中加入同一个表?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有下表,每个员工都有一个经理,如果一个人是他自己的经理,而managerid字段为null,我希望emid name and manager id
因为结果表名称是employee
Im having the following table , each employee have a manager, if a guy is his own manager den the managerid field is null, i want emid name and manager id
as result table name is employee
emid name managerid
1 raj null
2 ram 1
3 ravi null
4 arvind 3
5 rithu 2
我希望结果为
emid name managername
1 raj raj
2 ram raj
3 ravi ravi
4 arvind ravi
5 rithu ram
推荐答案
SELECT e1.emid, e1.name, COALESCE(e2.name, e1.name) AS managername
FROM employee e1
LEFT JOIN employee e2
ON e1.managerid = e2.emid
这篇关于在MySQL中加入同一个表?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文