使用SQL语句查询多级菜单中某级子集的所有父级

使用SQL语句查询多级菜单中某级子集的所有父级

SELECT
        t2.id
        FROM
        (
        SELECT
        @r AS _id,
        ( SELECT @r := PARENT_ID FROM pts_mat_type WHERE id = _id ) AS parent_id,
        @l := @l + 1 AS lvl
        FROM
        ( SELECT @r := #{typeId}, @l := '0' ) vars,
        pts_mat_type h
        WHERE
        @r<![CDATA[<>]]> '0'
        ) T1
        JOIN pts_mat_type T2 ON T1._id = T2.id
        ORDER BY
        T1.lvl DESC;
posted @ 2022-11-10 10:00  __先森  阅读(473)  评论(0编辑  收藏  举报