mysql根据子id查出所有上级父类

mysql代码

  表:t_b_dr_assets_catalog ,业务字段:parent为表中的上级id字段,2c9081c27337beb501733b876cbf0350 为主键id

  1、把 t_b_dr_assets_catalog换成自己的业务表

  2、parent换成自己业务表的上级id字段

  3、2c9081c27337beb501733b876cbf0350  要查询的子id数据

select t2.*
    from(
        select
            @r as _id,
            (select @r := parent from t_b_dr_assets_catalog where id = _id) as parent_id,
            @I := @I + 1 as lvl
      from
            (select @r :='2c9081c27337beb501733b876cbf0350',@I := 0) vars,
            t_b_dr_assets_catalog h
        ) t1
    join t_b_dr_assets_catalog t2
on t1._id = t2.id
ORDER BY id ;

 

 

结果:

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

posted @ 2023-02-12 19:29  下课后我要去放牛  阅读(564)  评论(0编辑  收藏  举报