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