Hi, I want to show the data as a way of hierarchy, as shown in the image below.

and here is my database table structure

and here is the query that I used, but its not the perfect result that I want
SELECT t1.parent_id AS primary_Id, t2.parent_id AS secondary_Id, t3.parent_id AS teritiary_Id FROM ucode AS t1 LEFT JOIN ucode AS t2 ON t2.parent_id = t1.id LEFT JOIN ucode AS t3 ON t3.parent_id = t2.parent_id
and the way out is

I do not need it
and another way that I tried
SELECT t1.parent_id AS primary_Id, t2.parent_id AS secondary_Id, t3.parent_id AS teritiary_Id FROM ucode AS t1 LEFT JOIN ucode AS t2 ON t2.parent_id = t1.id LEFT JOIN ucode AS t3 ON t3.parent_id = t2.id
and the way out is

Can you give me the right solution for this.
source share