with recursive t1(id, parent_id) as ( select*from blog_temp.etc_test t0 where t0.parent_id is null -- Anchor member. union all select t2.id, t2.parent_id from blog_temp.etc_test t2, t1 -- Recursive member. where t2.parent_id = t1.id ) select lpad(codename,(length(codename)+v.dict_level*4),' ') codename1 ,lpad(id,(length(id)+v.dict_level*4),' ') id1 , v.* from tree_dict v ;