0

Can anyone help me to show the second table from the first table in Mysql. I cannot understand how to approach the problem. I know the concept of left join and order by, but still cannot figure it out.

BTREE

1

Input Table:

Image1

Desired Output:

Image2

Nishant Gupta
  • 3,367
  • 1
  • 9
  • 17

1 Answers1

0

Use recursive CTE:

with recursive managers as  (
  select EmployeeId, name as 'Employee', 'Super Boss' as 'Manager', 1 as Level, ManagerId
  from employees
  where ManagerId is null
  union
  select e.EmployeeId, e.name, m.Employee, Level+1, e.ManagerId
  from employees e
    join managers m on m.EmployeeId=e.ManagerId
)
select Employee, Manager, Level 
from managers
slaakso
  • 7,222
  • 2
  • 14
  • 27
  • I don't know how recursive CTE works in Mysql but I will look into it and try to understand it. Thank you so much for the answer anyway. – Reeju Ballabh Jun 03 '22 at 13:24