mysql查询祖类_mysql 祖祖辈辈、子子孙孙(上级,下级)递归查询

子子孙孙:

CREATE DEFINER=``@`` FUNCTION `getChildPostList`(`userId` int(11),`projectId` varchar(32)) RETURNS varchar(1000) CHARSET latin1

BEGIN

DECLARE sTemp VARCHAR(1000);

DECLARE sTempChd VARCHAR(1000);

DECLARE res VARCHAR(1000);

DECLARE project VARCHAR(32);

DECLARE selfPost VARCHAR(32);

SET sTemp = '';

SET project = projectId ;

SET sTempChd =(select GROUP_CONCAT(post_id) from tb_post_user where user_id=userId);

set selfPost=sTempChd;

IF project is not null then

WHILE sTempChd is not null DO

SELECT group_concat(post_id) INTO sTempChd FROM tb_team_post t

join tb_project_team t1 on t.team_id=t1.team_id

where t1.project_id=project and FIND_IN_SET(parent_id,sTempChd)>0;

IF sTempChd is not null then

if sTemp = '' then

SET sTemp =sTempChd;

else

SET sTemp = concat(sTemp,',',sTempChd);

end if ;

end if ;

END WHILE;

ELSE

WHILE sTempChd is not null DO

SELECT group_concat(post_id) INTO sTempChd FROM tb_team_post t where FIND_IN_SET(parent_id,sTempChd)>0;

IF sTempChd is not null then

if sTemp = '' then

SET sTemp =sTempChd;

else

SET sTemp = concat(sTemp,',',sTempChd);

end if ;

end if ;

END WHILE;

END IF;

RETURN sTemp;

END

祖祖辈辈:

SELECT GROUP_CONCAT(T2.post_name ORDER BY T2.create_time)

FROM (

SELECT

@r AS _post_id,

(SELECT @r := parent_id FROM tb_team_post WHERE post_id = _post_id) AS parent_id

FROM

(SELECT @r := #{postId}, @l := 0) vars,

tb_team_post h

WHERE @r <>-1) T1

JOIN tb_team_post T2

ON T1._post_id = T2.post_id

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值