CREATE FUNCTION `getGroupCID`(pid varchar(5000)) RETURNS VARCHAR(5000) DETERMINISTIC BEGIN DECLARE children VARCHAR(5000); SELECT group_concat(id) INTO children FROM group_info WHERE FIND_IN_SET(group_info.pid, pid); IF children IS NOT NULL THEN RETURN CONCAT(children, ',', getGroupCID(children)); ELSE RETURN ''; END IF; RETURN ''; END