mysql 查询 所有 父节点 单表
SELECT T2.* FROM ( SELECT @r AS _id, ( SELECT @r := parent_id FROM tp_module_rel WHERE REL_ID = _id ) AS parent_id, @l := @l + 1 AS lvl FROM (SELECT @r := 254, @l := 0) vars, tp_module_rel h WHERE @r <> 0 ) T1 JOIN tp_module_rel T2 ON T1._id = T2.rel_id ORDER BY T1.lvl DESC;
使用了临时表 。
在别的博客里面看到,忘了地址。。 记下 做备用
SELECTT2.*FROM(SELECT@r AS _id,(SELECT@r := parent_idFROMtp_module_relWHEREREL_ID = _id) AS parent_id,@l := @l + 1 AS lvlFROM(SELECT @r := 254, @l := 0) vars,tp_module_rel hWHERE@r <> 0) T1JOIN tp_module_rel T2 ON T1._id = T2.rel_idORDER BYT1.lvl DESC;