mysql custom function to get tree structure data

In actual business, we often encounter tree-shaped institutional data, such as administrative divisions, column classifications, and so on.

The design organization of the database table is roughly as follows:

    

CREATE TABLE `ri_sys_cat` (
  `cat_id` varchar(64) NOT NULL COMMENT 'ID',
  `cat_kind` varchar(4) DEFAULT NULL COMMENT 'Industry classification type',
  `cat_code` varchar(10) DEFAULT NULL COMMENT 'Industry classification code',
  `cat_name` varchar(100) NOT NULL COMMENT 'Category name',
  `parent_id` varchar(64) NOT NULL COMMENT '父类ID',
  PRIMARY KEY (`cat_id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COMMENT='Public industry classification table';

 

 

    When actually reading the table data, it is either processed on the java side, or the corresponding mysql function or stored procedure is written for processing.

 

    An example of java processing is as follows:

 

//Define the corresponding tree structure class
public class SysCatTree implements java.io.Serializable{
    
    private static final long serialVersionUID = 3118551510633166045L;
    
    /** Category ID*/
    private String catId;
    /** Category ID - catId alias */
    private String id;
    
    /** 01-National Economic Industry Classification*/
    private String catKind;
    
    /** Category Name*/
    private String catName;
    /** Category name - catName alias */
    private String label;
    
    /** Parent class ID*/
    private String parentId;
   
    /** Industry code */
    private String catCode;
    /** Subclass*/
    private List<SysCatTree> children;
	
    public void setCatId(String catId) {
        this.catId = catId;
        this.id = catId;
    }
    
    public String getCatName() {
        return catName;
    }
    public void setCatName(String catName) {
        this.catName = catName;
        this.label = catName;
    }
    //...omit get/set methods

}

   Get the corresponding database data and convert the list into a tree structure, the code is as follows

 

 

private static List<SysCatTree> createTree(List<SysCatTree> menuList, String parentId) {  
        List<SysCatTree> childMenu = new ArrayList<>();
        for (SysCatTree catTree : menuList) {
            if (parentId.equals(catTree.getParentId())) {
                List<SysCatTree> c_node = createTree(menuList, catTree.getId());  
                catTree.setChildren(c_node);
                childMenu.add(catTree);  
            }  
        }  
        return childMenu;  
 }  

   In our business, the tree code is stored in the database table, not the tree id. At this time, we need to query all the subclass codes below it according to the code code, and perform corresponding data queries. In order to meet this requirement, a custom function is required in the database.

 

   The custom mysql function is as follows

 

DROP FUNCTION IF EXISTS queryChildSysCatByCode;

CREATE FUNCTION `queryChildSysCatByCode`(catKind VARCHAR(4),catcode VARCHAR(10)) RETURNS varchar(2000) CHARSET utf8
    COMMENT 'Query the subclass code tree under the code according to the classification code'
BEGIN

-- define local variables
DECLARE sTempId VARCHAR(2000);
DECLARE sTempCode VARCHAR(500);
DECLARE sTempChildCode VARCHAR(2000);

-- assign an initial value to sTempId
select cat_id into sTempId from ri_sys_cat where cat_code = catcode;
-- loop get
WHILE sTempId IS NOT NULL DO
	select GROUP_CONCAT(cat_id),GROUP_CONCAT(cat_code) into sTempId,sTempCode from ri_sys_cat where cat_kind = catKind and  FIND_IN_SET(parent_id,sTempId)>0;

	IF sTempChildCode IS NULL AND sTempCode IS NOT NULL
		THEN set sTempChildCode = sTempCode;
	ELSEIF sTempChildCode	IS NOT NULL AND sTempCode IS NOT NULL
		THEN set sTempChildCode = CONCAT(sTempChildCode,',',sTempCode);
	END IF;
END WHILE;

return sTempChildCode;

 

 

   call as follows

    

select queryChildSysCatByCode('04','RS01');

   

   The returned results are as follows:

   

RS0101,RS0102,RS0103,RS0104,RS0101001,RS0101002,RS0101003,RS0101004,RS0101005,RS0101006,RS0101007,RS0102001,RS0102002,RS0102003,RS0102004,RS010301,RS010302,RS010303

 

   

 

 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=326067967&siteId=291194637