MySql8 WITH RECURSIVE recursive query parent-child collection method

MySql8 WITH RECURSIVE recursive query parent-child collection method

background

When developing a feature similar to comments, you need to query a subset of all comments when necessary. The implementation methods are different in different databases. This article uses the MySQL database, version 8.0

  • START [Param] CONNECT BY PRIOR can be used in Oracle database
  • In MySQL, you need to use WITH RECURSIVE

need

Find the children and grandchildren whose name is Zhang San. The pid is the parent id of the current record. For example, the pid of Zhang San’s son is Zhang San’s id, and so on.

insert image description here

Introduction

Calculate the cumulative result from 1 to 100.
WITH RECURSIVE t(n) AS ( //t is our result table, n is the field, you can specify only one field to indicate that no field is specified VALUES (1) //The start of recursion, at this point it can be understood that the t table field n has only one record 1
 UNION ALL
  SELECT n+1 FROM t WHERE n < 100   
  /*The result here is 2. At this time, the field n of the t table has two records, 1 and 2 respectively.
  * 3
  * ...
  * 100
  */													  
)
SELECT sum(n) FROM t; //Sum field n

Father prays for son

WITH RECURSIVE temp AS ( // Name the result table temp
	SELECT * FROM resource r WHERE r.name = '张三' //Query the record of the parent id. At this time, this record already exists in the temp table, as shown in Figure 1-1
	UNION ALL
	/*At this time, please note that the following sql is the last two records in the expected result (excluding the first one)
	*Note the condition after where, we use the id of the only record in the temp table to associate the pid in the resource table
	*The second record id of temp will be matched only when the first record of temp does not match the pid in the resource table */
	SELECT r.* FROM resource r,temp t WHERE t.id = r.pid
)select * from temp 

insert image description here

Son investigates father

WITH recursive temp AS (
	SELECT * FROM resource r WHERE r.name = '张三孙子'
	UNION ALL
	//It is known that it is a subset, so we need to match the resource id through the temp pid
	SELECT r.* FROM resource r,temp t WHERE t.pid = r.id
)select * from temp 

insert image description here

This is the end of this article about the method of recursively querying parent-child sets in MySql8. For more relevant content about recursively querying parent-child sets in MySql8, 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:
  • Detailed explanation of ensuring the consistency of MySQL views (with check option)
  • How to solve the error "A Windows service with the name MySQL already exists." when installing MySQL
  • TIMESTAMP with implicit DEFAULT value is deprecated error in MySQL 5.6
  • Solution to MySQL error TIMESTAMP column with CURRENT_TIMESTAMP
  • MySQL Tips: Solution to the problem of server quit without updating PID file
  • PHP SQL Injection with MySQL
  • MySQL 8.0 WITH query details

<<:  HTML is the central foundation for the development of WEB standards

>>:  Vue uniapp realizes the segmenter effect

Recommend

Install Docker for Windows on Windows 10 Home Edition

0. Background Hardware: Xiaomi Notebook Air 13/In...

Implementing image fragmentation loading function based on HTML code

Today we will implement a fragmented image loadin...

JavaScript function call, apply and bind method case study

Summarize 1. Similarities Both can change the int...

JavaScript implements select all and unselect all operations

This article shares the specific code for JavaScr...

JavaScript to achieve simple drag effect

This article shares the specific code of JavaScri...

Detailed explanation of CSS margin collapsing

Previous This is a classic old question. Since a ...

Understand CSS3 Grid layout in 10 minutes

Basic Introduction In the previous article, we in...

An article to understand the use of proxies in JavaScript

Table of contents What is an agent Basic knowledg...

How to use Vue's idea to encapsulate a Storage

Table of contents background Function Purpose Ide...

The most common declaration merge in TS (interface merge)

Table of contents 1. Merge interface 1.1 Non-func...

VMware15.5 installation Ubuntu20.04 graphic tutorial

1. Preparation before installation 1. Download th...

MySQL transaction details

Table of contents Introduction Four characteristi...