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

Best Practices Guide for MySQL Partitioned Tables

Preface: Partitioning is a table design pattern. ...

Implementation of MySQL Shell import_table data import

Table of contents 1. Introduction to import_table...

Detailed explanation of CSS margin overlap and solution exploration

I recently reviewed some CSS-related knowledge po...

The difference between absolute path and relative path in web page creation

1. Absolute path First of all, on the local compu...

What does input type mean and how to limit input

Common methods for limiting input 1. To cancel the...

A brief discussion on three methods of asynchronous replication in MySQL 8.0

In this experiment, we configure MySQL standard a...

Stealing data using CSS in Firefox

0x00 Introduction A few months ago, I found a vul...

CSS to achieve chat bubble effect

1. Rendering JD Effect Simulation Effect 2. Princ...

Javascript closure usage scenario principle detailed

Table of contents 1. Closure 2. Closure usage sce...

Which loop is the fastest in JavaScript?

Knowing which for loop or iterator is right for o...

Docker learning: the specific use of Container containers

Container is another core concept of Docker. Simp...

Summary and examples of vue3 component communication methods

The communication modes of vue3 components are as...

js to achieve waterfall flow layout (infinite loading)

This article example shares the specific code of ...