嗨我需要从下表中获取数据,我只传递id 5,但我需要获得所有5行(至于orgid 5,批准org是4,所以我们选择它,类似于orgid 4,批准org是3,它一直持续到批准组织0)
ORG_ID name APPROVING_ORG_ID
1 name 1 0
2 name2 1
3 name 3 2
4 name 4 3
5 name 5 4
答案 0 :(得分:0)
尝试在sql中创建函数。这可能会帮助你
CREATE DEFINER=`root`@`localhost` FUNCTION `GetAllNode`(GivenID INT) RETURNS text CHARSET latin1
DETERMINISTIC
BEGIN
DECLARE rv,q,queue,queue_children TEXT;
DECLARE queue_length,front_id,pos INT;
SET rv = '';
SET queue = GivenID;
SET queue_length = 1;
WHILE queue_length > 0 DO
SET front_id = queue;
IF queue_length = 1 THEN
SET queue = '';
ELSE
SET pos = LOCATE(',',queue) + 1;
SET q = SUBSTR(queue,pos);
SET queue = q;
END IF;
SET queue_length = queue_length - 1;
SELECT IFNULL(qc,'') INTO queue_children
FROM (SELECT GROUP_CONCAT(CAST(ORG_ID AS CHAR(50))) AS qc
FROM `yourtablename` WHERE `APPROVING_ORG_ID` = front_id) A ;
IF LENGTH(queue_children) = 0 THEN
IF LENGTH(queue) = 0 THEN
SET queue_length = 0;
END IF;
ELSE
IF LENGTH(rv) = 0 THEN
SET rv = queue_children;
ELSE
SET rv = CONCAT(rv,',',queue_children);
END IF;
IF LENGTH(queue) = 0 THEN
SET queue = queue_children;
ELSE
SET queue = CONCAT(queue,',',queue_children);
END IF;
SET queue_length = LENGTH(queue) - LENGTH(REPLACE(queue,',','')) + 1;
END IF;
END WHILE;
RETURN rv;
END