码迷,mamicode.com
首页 > 数据库 > 详细

MySql 根据节点查询父节点或子节点

时间:2020-03-31 12:44:32      阅读:289      评论:0      收藏:0      [点我收藏+]

标签:int   arch   mysq   arc   efault   default   find   mysql   pre   

根据节点查询所有子节点
CREATE DEFINER=`root`@`localhost` FUNCTION `getChildList`(rootId varchar(100)) RETURNS varchar(2000) CHARSET latin1
BEGIN   
DECLARE str varchar(2000);  
DECLARE cid varchar(100);   
SET str = $;   
SET cid = rootId;   
WHILE cid is not null DO   
    SET str = concat(str, ,, cid);   
    SELECT group_concat(id) INTO cid FROM tree where FIND_IN_SET(parent_id, cid) >0;   
END WHILE;   
RETURN str;   
END

select * from tree where FIND_IN_SET(Id,getChildList(feca4059-cb86-4dd2-8ef9-aee6c06c4517))
根据节点查询所有父节点
CREATE DEFINER=`root`@`localhost` FUNCTION `getParentList`(rootId varchar(100)) RETURNS varchar(1000) CHARSET latin1
BEGIN   
DECLARE fid varchar(100) default ‘‘;   
DECLARE str varchar(1000) default rootId;   
  
WHILE rootId is not null  do   
    SET fid =(SELECT parent_id FROM tree WHERE id = rootId);   
    IF fid is not null THEN   
        SET str = concat(str, ,, fid);   
        SET rootId = fid;   
    ELSE   
        SET rootId = fid;   
    END IF;   
END WHILE;   
return str;  
END

select * from tree where FIND_IN_SET(Id,getParentList(589bde6b-b432-4563-9101-8ec93161e398))

 

MySql 根据节点查询父节点或子节点

标签:int   arch   mysq   arc   efault   default   find   mysql   pre   

原文地址:https://www.cnblogs.com/Loners/p/12604146.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!