MYSQL递归获取子集 或者 父集
-- 起始值为2,获得所有父节点(包括自己)
SELECT *
FROM (
SELECT
@r AS _guid,
(SELECT @r := superguid FROM `table` WHERE guid = _guid) AS superguid,
@l := @l + 1 AS lvl
FROM
(SELECT @r := '000', @l := 0) vars,
`table` h
) T1
JOIN `table` T2
ON T1._guid = T2.guid
ORDER BY T1.lvl DESC
select t.* from (
select @guid idlist,
(select @guid:=group_concat(parentid separator ',') from table where find_in_set(guid,@guid)) sub
from table,(select @guid:='000') vars
where @guid is not null) tl,table t
where find_in_set(t.guid,tl.idlist);
-- 获取所有子子节点包括自己
select t.* from (
select @guid idlist,
(select @guid:=group_concat(guid separator ',') from table where find_in_set(parentid,@guid)) sub
from table,(select @guid:='000') vars
where @guid is not null) tl,table t
where find_in_set(t.guid,tl.idlist);