mysql method to recursively search for all child nodes of a menu node

mysql method to recursively search for all child nodes of a menu node

background

There is a requirement in the project to check all nodes of the menu node. After checking online, most methods use stored procedures. Since the online environment cannot add stored procedures casually,

Therefore, a similar recursive method is used here to query all child nodes of the menu.

Prepare

Create a menu table:

CREATE TABLE `menu` (
 `id` int(11) NOT NULL AUTO_INCREMENT COMMENT 'Menu id',
 `parent_id` int(11) DEFAULT NULL COMMENT 'parent node id',
 `menu_name` varchar(128) DEFAULT NULL COMMENT 'Menu name',
 `menu_url` varchar(128) DEFAULT '' COMMENT 'Menu path',
 `status` tinyint(3) DEFAULT '1' COMMENT 'Menu status 1-valid; 0-invalid',
 PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=12212 DEFAULT CHARSET=utf8;

Insert data:

INSERT INTO `menu` VALUES ('0', null, 'Menu0', ' ', '1');
INSERT INTO `menu` VALUES ('1', '0', 'Menu 1', '', '1');
INSERT INTO `menu` VALUES ('11', '1', 'Menu 11', '', '1');
INSERT INTO `menu` VALUES ('12', '1', 'Menu 12', '', '1');
INSERT INTO `menu` VALUES ('13', '1', 'Menu 13', '', '1');
INSERT INTO `menu` VALUES ('111', '11', 'Menu 111', '', '1');
INSERT INTO `menu` VALUES ('121', '12', 'Menu 121', '', '1');
INSERT INTO `menu` VALUES ('122', '12', 'Menu 122', '', '1');
INSERT INTO `menu` VALUES ('1221', '122', 'Menu 1221', '', '1');
INSERT INTO `menu` VALUES ('1222', '122', 'Menu 1222', '', '1');
INSERT INTO `menu` VALUES ('12211', '1222', 'Menu 12211', '', '1');

The resulting directory structure is shown below:

Query

First post the sql statement:

For example, to query all child nodes of menu node 12, the query results are:

analyze

First, analyze the statements after from, sort by parent_id and id, and use the menu node to be queried as a variable. The result after from is

Next, let’s look at the if (express1, express2, express3) conditional statement. The if statement is similar to a ternary operator. When exprss1 is true, express2 is executed, otherwise express3 is executed.

FIND_IN_SET(str,strlist),str is the string to be queried,strlist field name parameters are separated by "," such as (1,2,6,8), query the results containing (str) in the field (strlist), and return the result as null or record

If parent_id is in @pid, add parent_id to @pid and execute row by row. The execution process is shown in the following table:

At this time, the displayed id is all the child node ids of the menu id 12

This is the end of this article about mysql recursive search for all child nodes of a menu node. For more relevant mysql recursive search for menu nodes content, please search 123WORDPRESS.COM's previous articles or continue to browse the following related articles. I hope you will support 123WORDPRESS.COM in the future!

You may also be interested in:
  • Analysis of the principles of Mysql dirty page flush and shrinking table space
  • Recommend several MySQL related tools
  • A brief analysis of MySQL's lru linked list
  • MySQL Query Cache and Buffer Pool
  • A brief analysis of MySQL cardinality statistics
  • What is a MySQL tablespace?
  • How to locate MySQL slow queries
  • MySQL Flush-List and dirty page flushing mechanism

<<:  Write a dynamic clock on a web page in HTML

>>:  Detailed steps to install Nginx on Linux

Recommend

Implementation of Nginx configuration of multi-port and multi-domain name access

To deploy multiple sites on a server, you need to...

TinyEditor is a simple and easy-to-use HTML WYSIWYG editor

A few days ago, I introduced to you a domestic xh...

Problems encountered when uploading images using axios in Vue

Table of contents What is FormData? A practical e...

Detailed explanation of three ways to cut catalina.out logs in tomcat

1. Log4j for log segmentation 1) Prepare three pa...

How to install and configure the supervisor daemon under centos7

Newbie, record it yourself 1. Install supervisor....

Tutorial diagram of using Jenkins for automated deployment under Windows

Today we will talk about how to use Jenkins+power...

JavaScript event loop case study

Event loop in js Because JavaScript is single-thr...

How to filter out duplicate data when inserting large amounts of data into MySQL

Table of contents 1. Discover the problem 2. Dele...

A brief discussion on the difference between src and href in HTML

Simply put, src means "I want to load this r...

MySQL quickly inserts 100 million test data

Table of contents 1. Create a table 1.1 Create te...

Let's talk in depth about the principle and implementation of new in JS

Table of contents definition Constructor bodies a...

Supplementary article on front-end performance optimization

Preface I looked at the previously published arti...