I have previously written an article about recursive querying tree structures in MySQL using custom functions. Since MySQL 8.0, recursive query syntax has finally been supported. CTE First, let's understand what CTE is. The full name is Common Table Expressions WITH cte1 AS (SELECT a, b FROM table1), cte2 AS (SELECT c, d FROM table2) SELECT b, d FROM cte1 JOIN cte2 WHERE cte1.a = cte2.c; cte1, cte2 are the CTEs we defined, which can be referenced in the current query It can be seen that CTE is a temporary result set, similar to a derived table. The difference between the two is not explained in detail here. You can refer to the MySQL development documentation: https://dev.mysql.com/doc/refman/8.0/en/with.html#common-table-expressions-recursive-examples Recursive query Let's first look at the syntax of recursive queries. WITH RECURSIVE cte_name AS ( SELECT ... -- return initial row set UNION ALL / UNION DISTINCT SELECT ... -- return additional row sets ) SELECT * FROM cte;
The recursive part cannot include:
The above explanation may be a bit abstract, so let’s slowly understand it through examples. WITH RECURSIVE cte (n) AS -- The n defined here is equivalent to the column name of the result set, which can also be defined in the following query ( SELECT 1 UNION ALL SELECT n + 1 FROM table WHERE n < 5 ) SELECT * FROM cte; --result +------+ | n | +------+ | 1 | | 2 | | 3 | | 4 | | 5 | +------+
EXAMPLE Finally, let's look at an example of a tree structure. CREATE TABLE `c_tree` ( `id` int(11) NOT NULL AUTO_INCREMENT, `cname` varchar(255) COLLATE utf8mb4_unicode_ci DEFAULT NULL, `parent_id` int(11) DEFAULT NULL, PRIMARY KEY (`id`) ) ENGINE=InnoDB AUTO_INCREMENT=13 DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci; mysql> select * from c_tree; +----+---------+-----------+ | id | cname | parent_id | +----+---------+-----------+ | 1 | 1 | 0 | | 2 | 2 | 0 | | 3 | 3 | 0 | | 4 | 1-1 | 1 | | 5 | 1-2 | 1 | | 6 | 2-1 | 2 | | 7 | 2-2 | 2 | | 8 | 3-1 | 3 | | 9 | 3-1-1 | 8 | | 10 | 3-1-2 | 8 | | 11 | 3-1-1-1 | 9 | | 12 | 3-2 | 3 | +----+---------+-----------+ mysql> WITH RECURSIVE tree_cte as ( select * from c_tree where parent_id = 3 UNION ALL select t.* from c_tree t inner join tree_cte tcte on t.parent_id = tcte.id ) SELECT * FROM tree_cte; +----+---------+-----------+ | id | cname | parent_id | +----+---------+-----------+ | 8 | 3-1 | 3 | | 12 | 3-2 | 3 | | 9 | 3-1-1 | 8 | | 10 | 3-1-2 | 8 | | 11 | 3-1-1-1 | 9 | +----+---------+-----------+
More information https://dev.mysql.com/doc/refman/8.0/en/with.html The above is the full content of this article. I hope it will be helpful for everyone’s study. I also hope that everyone will support 123WORDPRESS.COM. You may also be interested in:
|
<<: A complete record of the process of building mobile applications using Vue Native
Preface Learn to create a file system on your sys...
Search Mirror docker search rocketmq View image v...
Table of contents Preface start Preface The defau...
Table of contents Common array methods Adding and...
introduction The previous article introduced the ...
The first method: dynamically add a class to show...
This article describes the sql_mode mode in MySQL...
Table of contents 1. Create components using func...
Table of contents Preface analyze Initial Renderi...
Write configuration files in server.xml and conte...
First: 4 ways to introduce CSS There are four way...
Get the current time: select current_timestamp; O...
This article example shares the specific code of ...
Several Differences Between MySQL 5.x and MySQL 8...
summary: The following is a method for changing t...