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

How to use jconsole to monitor remote Tomcat services

What is JConsole JConsole was introduced in Java ...

How to encapsulate WangEditor rich text component in Angular

The rich text component is a very commonly used c...

No-nonsense quick start React routing development

Install Enter the following command to install it...

Vue implements zip file download

This article example shares the specific code of ...

How to write high-quality JavaScript code

Table of contents 1. Easy to read code 1. Unified...

Tutorial on installing mysql5.7.23 on Ubuntu 18.04

This article shares with you the specific method ...

Use of TypeScript Generics

Table of contents 1. Easy to use 2. Using generic...

Steps to enable TLS in Docker for secure configuration

Preface I had previously enabled Docker's 237...

uni-app WeChat applet authorization login implementation steps

Table of contents 1. Application and configuratio...

Set the input to read-only via disabled and readonly

There are two ways to achieve read-only input: dis...

Common solutions for Mysql read-write separation expiration

The pitfalls of MySQL read-write separation The m...

Don't forget to close the HTML tag

Building web pages that comply with Web standards ...

Creating a Secondary Menu Using JavaScript

This article example shares the specific code of ...

Mini Programs use Mini Program Cloud to implement WeChat payment functions

Table of contents 1. Open WeChat Pay 1.1 Affiliat...