I have an employees
table in which all employees report to a superior except for one employee who reports to no one as he is the boss.
employeeNumber | reportsTo
---------------|-----------------
1002 | null
1056 | 1002
1143 | 1056
1165 | 1143
...
I want to be able to show the employee numbers of all employees who are superiors of a particular employee. (i.e. the person he reportsTo, and the employee who that person reportsTo, ... all the way up)
For e.g. employeeNumber 1165
should yield
| employeeNumber |
|----------------|
| null |
| 1002 |
| 1056 |
| 1143 |
I have written a MySQL statement which carries out the query but I believe this is a bit too complex to read and understand:
select reportsTo as employeeNumber from employees where employeeNumber in
(select reportsTo from employees where employeeNumber in (select reportsTo from employees where
employeeNumber = 1165)) union select reportsTo from employees where employeeNumber in
(select reportsTo from employees where employeeNumber = 1165) union select reportsTo from employees
where employeeNumber = 1165;
What simple MySQL statement can I use to get the same result?