MySQL查询所有上级信息

SELECT T2.id, T2.`code`
FROM ( 
    SELECT 
        @r AS _id, 
        (SELECT @r := parent_id FROM sys_position WHERE id = _id) AS parent_id, 
        @l := @l + 1 AS lvl 
    FROM 
        (SELECT @r := '83', @l := 0) vars,  
        sys_position h 
    WHERE @r != 0) T1 
JOIN sys_position T2 
ON T1._id = T2.id 
ORDER BY T1.lvl DESC

你可能感兴趣的:(mysql,数据库)