MySQL Query ID parent or child ID custom function achieved by recursively

background:

In MySQL if the level is limited, for example, we can determine in advance if the maximum depth of the tree, then all nodes in the depth of the tree roots will not exceed the maximum depth of the tree, then we can directly be realized by left join.

But many times we can not control or be aware of the depth of the tree. In this case it is necessary to achieve using MySQL stored procedures (functions) or used in a program implemented recursive. This article discusses the methods to use the function in MySQL to achieve:

First, prepare the environment

 

1, construction of the table

1 CREATE TABLE `table_name`  (
2   `id` int(11) NOT NULL AUTO_INCREMENT,
3   `status` int(255) NULL DEFAULT NULL,
4   `pid` int(11) NULL DEFAULT NULL,
5   PRIMARY KEY (`id`) USING BTREE
6 ) ENGINE = InnoDB AUTO_INCREMENT = 1 CHARACTER SET = utf8 COLLATE = utf8_general_ci ROW_FORMAT = Dynamic;

 

2, insert data

 1 INSERT INTO `table_name` VALUES (1, 12, 0);
 2 INSERT INTO `table_name` VALUES (2, 4, 1);
 3 INSERT INTO `table_name` VALUES (3, 8, 2);
 4 INSERT INTO `table_name` VALUES (4, 16, 3);
 5 INSERT INTO `table_name` VALUES (5, 32, 3);
 6 INSERT INTO `table_name` VALUES (6, 64, 3);
 7 INSERT INTO `table_name` VALUES (7, 128, 6);
 8 INSERT INTO `table_name` VALUES (8, 256, 7);
 9 INSERT INTO `table_name` VALUES (9, 512, 8);
10 INSERT INTO `table_name` VALUES (10, 1024, 9);
11 INSERT INTO `table_name` VALUES (11, 2048, 10);

 

Second, write MySQL function

 

1, the query all the parent of the current node

 1 delimiter // 
 2 CREATE FUNCTION `getParentList`(root_id BIGINT) 
 3      RETURNS VARCHAR(1000) 
 4      BEGIN 
 5           DECLARE k INT DEFAULT 0;
 6         DECLARE fid INT DEFAULT 1;
 7         DECLARE str VARCHAR(1000) DEFAULT '$';
 8         WHILE rootId > 0 DO
 9               SET fid=(SELECT pid FROM table_name WHERE root_id=id); 
10               IF fid > 0 THEN
11                   SET str = concat(str,',',fid);   
12                   SET root_id = fid;  
13               ELSE 
14                   SET root_id=fid;  
15               END IF;  
16      END WHILE;
17    RETURN str;
18  END  //
19  delimiter ;

 

2, query all child nodes of the current node

 1  
 2  delimiter //
 3  CREATE FUNCTION `getChildList`(root_id BIGINT) 
 4      RETURNS VARCHAR(1000) 
 5      BEGIN 
 6        DECLARE str VARCHAR(1000) ; 
 7        DECLARE cid VARCHAR(1000) ; 
 8        DECLARE k INT DEFAULT 0;
 9        SET str = '$'; 
10        SET cid = CAST(root_id AS CHAR);12        WHILE cid IS NOT NULL DO  
13                 IF k > 0 THEN
14                   SET str = CONCAT(str,',',cid);
15                 END IF;
16                 SELECT GROUP_CONCAT(id) INTO cid FROM table_name WHERE FIND_IN_SET(pid,cid)>0;
17                 SET k = k + 1;
18        END WHILE; 
19        RETURN str; 
20 END //  
21 delimiter ;

 

Third, the test

1, get all the parent of the current node

SELECT getParentList(10);

 

2, all bytes of the current node acquires

SELECT getChildList(3);

 

This article finished ......

Guess you like

Origin www.cnblogs.com/cmacro/p/11937341.html