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
The complete steps of Centos7 bridge network conf...
The action of the form is different from the URL j...
Lottie is an open source animation library for iO...
The dd and dt tags are used for lists. We usually...
Sometimes we save a lot of duplicate data in the ...
Table of contents Creating OAuth Apps Get the cod...
1. <div></div> and <span></s...
Install Apache from source 1. Upload the Apache s...
I won’t waste any more time talking nonsense, let...
Overview Today we will mainly share how to config...
Table of contents 1. Original Definition 2. JS op...
Achieve results Implementation Code html <div ...
Table of contents 1. Instructions 2. Modifiers 3....
1. Introduction I recently upgraded my blog and a...
Zabbix automatically discovers rules to monitor s...