MySQL child nodes query all parent nodes (query results include their own nodes)

MySQL child nodes query all parent nodes (query results include their own nodes)

SQL code

SELECT t2.id, t2.`menuname`
FROM
    (
        SELECT 
        @r AS _id,
        (SELECT @r := parentid FROM t_menu WHERE id = _id) AS parent_id,
        @l := @l + 1 AS lvl
        FROM
        (SELECT @r := '644', @l := 0) vars, t_menu AS h
        WHERE @r != 0 
    ) t1
JOIN t_menu t2
ON t1._id = t2.Id

@r := '644'

Enter the ID of the child node here

 search result

 page tree structure

Guess you like

Origin blog.csdn.net/sgx1825192/article/details/126392286