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

Solution to css3 transform transition jitter problem

transform: scale(); Scaling will cause jitter in ...

Teach you how to use vscode to build a react-native development environment

question The code has no prompt: Many non-front-e...

Pure JavaScript to implement the number guessing game

Develop a number guessing game that randomly sele...

Analysis of the reasons why MySQL field definitions should not use null

Why is NULL so often used? (1) Java's null Nu...

How to generate mysql primary key id (self-increment, unique and irregular)

Table of contents 1. Use the uuid function to gen...

Explanation of Truncate Table usage

TRUNCATE TABLE Deletes all rows in a table withou...

How to use translate and transition in CSS3

I always feel that translate and transition are v...

MySQL learning notes help document

View system help help contents mysql> help con...

Teach you about react routing in five minutes

Table of contents What is Routing Basic use of pu...

VMware + Ubuntu18.04 Graphic Tutorial on Building Hadoop Cluster Environment

Table of contents Preface VMware clone virtual ma...

How to deploy and start redis in docker

Deploy redis in docker First install Docker in Li...

MySQL 5.7.21 installation and password configuration tutorial

MySQL5.7.21 installation and password setting tut...