backgroundWhen 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
needFind 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. IntroductionCalculate 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 sonWITH 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 Son investigates fatherWITH 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 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:
|
<<: HTML is the central foundation for the development of WEB standards
>>: Vue uniapp realizes the segmenter effect
0. Background Hardware: Xiaomi Notebook Air 13/In...
Difference between HTML and XHTML 1. XHTML elemen...
Table of contents 1. Introduction to MHA 1. What ...
Today we will implement a fragmented image loadin...
Summarize 1. Similarities Both can change the int...
This article shares the specific code for JavaScr...
This article shares the specific code of JavaScri...
Previous This is a classic old question. Since a ...
Basic Introduction In the previous article, we in...
Table of contents What is an agent Basic knowledg...
Table of contents background Function Purpose Ide...
1. Create a repository in the specified directory...
Table of contents 1. Merge interface 1.1 Non-func...
1. Preparation before installation 1. Download th...
Table of contents Introduction Four characteristi...