Implementation method of Mysql tree recursive query

Implementation method of Mysql tree recursive query

Preface

For tree-structured data in the database, such as department tables, sometimes we need to know all the subordinate departments of a department or all the superior departments of a department. At this time, we need to use MySQL recursive query

I'm currently migrating a project from Oracle to MySQL. I encountered some Oracle functions that MySQL doesn't have, so I had to customize the functions or find a way to replace the functions for the transformation.

Oracle recursive query

If Oracle implements recursive query, you can use start with ... connect by

The basic syntax of connect by recursive query is:

select 1 from table start with ... connect by prior id = pId

start with: indicates what node is the root node. You can write 1=1 without restriction. To use the node with id 123 as the root node, write start with id =123

connect by: connect by is required, start with can be omitted in some cases, or start with 1=1 without restriction

Prior: The prior keyword can be placed before or after the equal sign, and the meaning is different. For example, prior id = pid means that pid is the root node of this record.

For details, please refer to an Oracle blog I wrote before: https://www.jb51.net/article/156306.htm

Oracle Implementation

<select id="listUnitInfo" resultType="com.admin.system.unit.model.UnitModel" databaseId="oracle">
 select distinct u.unit_code,
 u.unit_name,
 u.unit_tel,
 u.para_unit_code
 from lzcity_approve_unit_info u
 start with 1 = 1
 <if test="unitCode != null and unitCode !=''">
 and u.unit_code = #{unitCode}
 </if>
 <if test="unitName!=null and unitName!=''">
 and u.unit_name like '%'|| #{unitName} ||'%'
 </if>
 connect by prior u.unit_code = u.para_unit_code
 and u.unit_code <>u.para_unit_code
 </select>

Mysql recursive query

The following mainly introduces the implementation of MySQL. MySQL does not provide similar functions, so it can only be implemented through custom functions. There are a lot of such materials on the Internet, but I don’t know which one is original. This blog is well written, https://www.jb51.net/database/201209/152513.html. Below I also use the method provided by the author to implement my own. Thanks to the author for sharing.

Here we use the custom function provided by the author, plus the Find_in_set function find_in_set(u.unit_code,getunitChildList(#{unitCode})) , where getunitChildList is a custom function

<select id="listUnitInfo" resultType="com.admin.system.unit.model.UnitModel" databaseId="mysql">
 select distinct u.unit_code,
  u.unit_name,
  u.unit_tel,
  u.para_unit_code
  from t_unit_info u
  <where>
  <if test="unitCode != null and unitCode !=''">
  and find_in_set(u.unit_code,getunitChildList(#{unitCode}))
  </if>
  <if test="unitName!=null and unitName!=''">
  and u.unit_name like concat('%', #{unitName} ,'%')
  </if>
  </where>
 </select>

getUnitChildList custom function

DELIMITER $$

USE `gd_base`$$

DROP FUNCTION IF EXISTS `getUnitChildList`$$

CREATE DEFINER=`root`@`%` FUNCTION `getUnitChildList`(rootId INT) RETURNS VARCHAR(1000) CHARSET utf8
BEGIN
 DECLARE sChildList VARCHAR(1000);
 DECLARE sChildTemp VARCHAR(1000);
 SET sChildTemp = CAST(rootId AS CHAR);
 WHILE sChildTemp IS NOT NULL DO
 IF (sChildList IS NOT NULL) THEN
  SET sChildList = CONCAT(sChildList,',',sChildTemp);
 ELSE
 SET sChildList = CONCAT(sChildTemp);
 END IF;
 SELECT GROUP_CONCAT(unit_code) INTO sChildTemp FROM LZCITY_APPROVE_UNIT_INFO WHERE FIND_IN_SET(para_unit_code,sChildTemp)>0;
 END WHILE;
 RETURN sChildList;
END$$

DELIMITER ;

Summarize

The above is the full content of this article. I hope that the content of this article will have certain reference learning value for your study or work. If you have any questions, you can leave a message to communicate. Thank you for your support for 123WORDPRESS.COM.

You may also be interested in:
  • How to implement tree-like query of all child nodes in MySQL
  • MySQL multi-level structure-tree search introduction
  • MySQL recursive query tree table child nodes, parent nodes specific implementation
  • MySQL query tree structure method

<<:  CentOS 8 installation diagram (super detailed tutorial)

>>:  Detailed explanation of JS memory space

Recommend

Detailed explanation of MySQL Explain

In daily work, we sometimes run slow queries to r...

Summary of the use of TypeScript in React projects

Preface This article will focus on the use of Typ...

Detailed explanation of various usages of proxy_pass in nginx

Table of contents Proxy forwarding rules The firs...

JavaScript realizes the effect of mobile modal box

This article example shares the specific code of ...

Chrome monitors cookie changes and assigns values

The following code introduces Chrome's monito...

Detailed explanation of the binlog log analysis tool for monitoring MySQL: Canal

Canal is an open source project under Alibaba, de...

In-depth analysis of HTML semantics and its related front-end frameworks

About semantics Semantics is the study of the rel...

A simple way to put HTML footer at the bottom of the page

Requirement: Sometimes, when the page content is ...

impress.js presentation layer framework (demonstration tool) - first experience

I haven’t blogged for half a year, which I feel a ...

React High-Order Component HOC Usage Summary

One sentence to introduce HOC What is a higher-or...

How to use echarts to visualize components in Vue

echarts component official website address: https...

How to improve MySQL Limit query performance

In MySQL database operations, we always hope to a...

Nest.js authorization verification method example

Table of contents 0x0 Introduction 0x1 RBAC Imple...