mysql树查询,sql递归函数
2012-05-23 10:32 javaspring 阅读(448) 评论(0) 编辑 收藏 举报
mysql树查询,sql递归函数
select cateID from goodscategory where FIND_IN_SET(cateID ,getGoodsChildLst('43242'))
CREATE FUNCTION `getGoodsChildLst`(rootId CHAR(32))
RETURNS varchar(1000)
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(cateID) INTO sTempChd FROM goodscategory where FIND_IN_SET(parentCateID,sTempChd)>0;
END WHILE;
RETURN sTemp;
END
select cateID from goodscategory where FIND_IN_SET(cateID ,getGoodsChildLst('43242'))
CREATE FUNCTION `getGoodsChildLst`(rootId CHAR(32))
RETURNS varchar(1000)
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(cateID) INTO sTempChd FROM goodscategory where FIND_IN_SET(parentCateID,sTempChd)>0;
END WHILE;
RETURN sTemp;
END