mysql uses stored procedures to implement tree node acquisition method

mysql uses stored procedures to implement tree node acquisition method

As shown in the figure:

Table Data

For such a tree, how to get all the nodes under "Gao Yinrui" (one SQL statement is definitely not enough)

Write via stored procedure

DELIMITER //
CREATE FUNCTION `getChildLst`(rootId INT)
 
RETURNS varchar(1000) READS SQL DATA
 
BEGIN
 DECLARE sTemp VARCHAR(1000);
 
 DECLARE sTempChd VARCHAR(1000);
 
 SET sTemp = '$';
 
 SET sTempChd =cast(rootId as CHAR);
 
 WHILE sTempChd is not null DO
 
 SET sTemp = concat(sTemp,',',sTempChd);
 
 SELECT group_concat(id) INTO sTempChd FROM document_file_name where FIND_IN_SET(pId,sTempChd)>0;
 
 END WHILE;
 
 RETURN sTemp;
 
END //

Create the above stored procedure

select * from document_file_name where find_in_set(id, getChildLst(1)); 

Summarize

This is the end of this article about how to use stored procedures in MySQL to get tree nodes. For more information about how to get tree nodes in MySQL stored procedures, please search for previous articles on 123WORDPRESS.COM or continue to browse the following related articles. I hope you will support 123WORDPRESS.COM in the future!

You may also be interested in:
  • Simple writing of MYSQL stored procedures and functions
  • Detailed example of MySQL data storage process parameters
  • Two ways to write stored procedures in Mysql with and without return values
  • Detailed explanation of MySQL stored procedures, cursors, and transaction examples
  • How to create a stored procedure in MySQL and add records in a loop
  • Detailed explanation of the entry-level use of MySql stored procedure parameters

<<:  Native js custom right-click menu

>>:  js to achieve cool fireworks effect

Recommend

Detailed explanation of querying JSON format fields in MySQL

During the work development process, a requiremen...

Invalid solution when defining multiple class attributes in HTML

In the process of writing HTML, we often define mu...

Difference between var and let in JavaScript

Table of contents 1. Scopes are expressed in diff...

Summary of the differences between Html, sHtml and XHtml

For example: <u> This has no ending characte...

How to install golang under linux

Go is an open source programming language that ma...

Method of building redis cluster based on docker

Download the redis image docker pull yyyyttttwwww...

Installation tutorial of MySQL 5.1 and 5.7 under Linux

The operating system for the following content is...

Vue implements three-dimensional column chart based on echarts

The three-dimensional column chart consists of th...

Summary of tips for setting the maximum number of connections in MySQL

Method 1: Command line modification We only need ...

Zabbix uses PSK shared key to encrypt communication between Server and Agent

Since Zabbix version 3.0, it has supported encryp...

iframe adaptive size implementation code

Page domain relationship: The main page a.html bel...

24 Practical JavaScript Development Tips

Table of contents 1. Initialize the array 2. Arra...

Several ways to submit HTML forms_PowerNode Java Academy

Method 1: Submit via the submit button <!DOCTY...

Webpack loads css files and its configuration method

webpack loads css files and its configuration Aft...

Simply understand the writing and execution order of MySQL statements

There is a big difference between the writing ord...