mysql创建函数之省市区数据查询

时间:2023-01-28 04:17:48

DROP FUNCTION IF EXISTS queryChildrenAreaInfo;
CREATE FUNCTION queryChildrenAreaInfo(areaId varchar(20),area_level int)
RETURNS VARCHAR(4000)
BEGIN
DECLARE sTemp VARCHAR(4000);
DECLARE sTempChd VARCHAR(4000);

SET sTemp='$';
SET sTempChd = areaId;

WHILE sTempChd IS NOT NULL DO
SET sTemp= CONCAT(sTemp,',',sTempChd);
SELECT GROUP_CONCAT(area_id) INTO sTempChd FROM area WHERE FIND_IN_SET(Parentarea_id,sTempChd)>0 and area_type=area_level;
END WHILE;
RETURN sTemp;
END;

查找所有子级的编号的函数

CREATE FUNCTION getParentList(rootId VARCHAR(100))
RETURNS VARCHAR(1000)
BEGIN
DECLARE fid varchar(100) default '';
DECLARE str varchar(1000) default rootId;

WHILE rootId is not null do
SET fid =(SELECT parent_user_id FROM recommended_tree WHERE user_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