PrefaceThis article uses the new features of MySQL 8.0 to implement recursive queries. Detailed example code is given in the article. Let's take a look at the detailed introduction. Mysql8.0 recursive query usageThe table data is as follows
1. We need to find all subcategories under the "clothing" category with recursive type_cte as ( select * from t_category where cat_id = 4 union all select t.* from t_category t inner join type_cte type_cte2 on t.parent_cid = type_cte2.cat_id ) select cat_id, name, parent_cid from type_cte
2. Query all subcategories under the "Beauty" category, and the category name includes the name of the parent category with recursive type_cte as ( select cat_id,name,parent_cid from t_category where cat_id = 12 union all select t.cat_id,concat(type_cte2.name,'>',t.name),t.parent_cid from t_category t inner join type_cte type_cte2 on t.parent_cid = type_cte2.cat_id ) select cat_id, name, parent_cid from type_cte;
3. Query all parent categories of a category Just adjust the SQL according to the second question. with recursive type_cte as ( select cat_id,name,parent_cid from t_category where cat_id = 40 union all select t.cat_id,concat(type_cte2.name,'>',t.name),t.parent_cid from t_category t inner join type_cte type_cte2 on t.cat_id = type_cte2.parent_cid ) select cat_id, name, parent_cid from type_cte;
SummarizeThis is the end of this article about MySQL 8.0 recursive query. For more relevant MySQL 8.0 recursive query content, please search for previous articles on 123WORDPRESS.COM or continue to browse the following related articles. I hope everyone will support 123WORDPRESS.COM in the future! You may also be interested in:
|
<<: How to solve the background tiling and border breaking of CSS style div or li in IE6
>>: How to use positioning to center elements (web page layout tips)
Use navicat to test and learn: First use set auto...
Preface Nginx is an HTTP server designed for perf...
Preface During the development process, you will ...
Say goodbye to the past Before vscode had remote ...
Copy code The code is as follows: <!--[if IE]&...
1. Summary: In general, they can be divided into ...
This article records the graphic tutorial of MySQ...
Preface The company's Ubuntu server places th...
How to indicate the parent directory ../ represent...
Table of contents Installing the SDK Managing loc...
The custom encapsulation code of the vue button c...
Table of contents design Component Communication ...
A colleague reported that a MySQL instance could ...
A hyperlink URL in Vm needs to be concatenated wit...
<br />In previous tutorials of 123WORDPRESS....