0

Below is my table structure

Id FirstName ReportingTo

6 A

9 B 6

10 C 6

11 D 9

12 E 9

16 F 11

20 G 12

23 H 20

This is my table structure, I want an output that will give me a result from bottom to top hierarchy. Means if I put Id = 23 then it should be give - (20,12,9,6). As I am using MySQL 5.7 so With Recursive function I can't use.

select id, firstname, reporting_to from (select * from users order by
id) products_sorted, (select @pv := '23') initialisation where
find_in_set(reporting_to, @pv) and length(@pv := concat(@pv, ',',
id))

But it gives only a single row. I need the entire hierarchy from bottom to top.

Dharman
  • 30,962
  • 25
  • 85
  • 135

0 Answers0