# 向下递归
select
group_concat( ids._id )
FROM
(
SELECT
@r AS _id,
(
SELECT
@r := group_concat( c.id )
FROM
t_sys_material_category_copy1 a
LEFT JOIN t_sys_material_category_copy1 c ON a.id = c.id
WHERE
FIND_IN_SET ( a.parent_code, _id )
) AS parent_id,
@l := @l + 1 AS lvl
FROM
( SELECT @r := '123456', @l := 0 ) vars,
t_sys_material_category_copy1 b
WHERE
@r != 0
) ids
#向上递归
SELECT
GROUP_CONCAT( ids.id SEPARATOR "-" )
FROM
(
SELECT
T2.id
FROM
(
SELECT
@r AS _id,
( SELECT @r := parent_code FROM t_sys_material_category_copy1 WHERE id = _id ) AS 2v2,
@l := @l + 1 AS lvl
FROM
( SELECT @r := '123456' ) vars,
t_sys_material_category_copy1 h
WHERE
@r != 0
) T1
JOIN t_sys_material_category_copy1 T2 ON T1._id = T2.id
ORDER BY
T2.id
) ids