mysql查询当前节点的所有父节点信息

SELECT T2.id, T2.name, T2.pid
FROM ( 
SELECT 
@r AS _id, 
(SELECT @r := pid FROM digui WHERE id = _id) AS pid, 
@l := @l + 1 AS lvl 
FROM 
(SELECT @r := 6, @l := 0) vars, 
digui h 
WHERE @r <> 0) T1 
JOIN digui T2 
ON T1._id = T2.id

digui:  表名

6:就是要查的节点

pid:父节点字段

你可能感兴趣的:(mysql)