mysql 层级递归

CREATE  FUNCTION `getCateIdsList`(rootId INT) RETURNS varchar(1000) CHARSET utf8
BEGIN
      DECLARE sTemp VARCHAR(1000);
    DECLARE sTempChd VARCHAR(1000);

    SET sTemp = '$';
    SET sTempChd =cast(rootId as CHAR);

    WHILE sTempChd is not null DO
        SET sTemp = concat(sTemp,',',sTempChd);
        SELECT group_concat(id) INTO sTempChd FROM  t_goods_cate where FIND_IN_SET(parent_id,sTempChd)>0;
    END WHILE;
    RETURN sTemp; 
END

你可能感兴趣的:(mysql 层级递归)