Mysql8.0递归查询的简单用法示例

吾爱主题 阅读:115 2024-04-02 08:04:30 评论:0

前言

本文使用Mysql8.0的特新实现递归查询,文中给出了详细的实例代码,下面话不多说了,来一起看看详细的介绍吧

Mysql8.0递归查询用法

表数据如下

+--------+----------+------------+
| cat_id | name     | parent_cid |
+--------+----------+------------+
|     12 | 美妆     |          0 |
|      4 | 服装     |          0 |
|      5 | 女装     |          4 |
|      6 | 男装     |          4 |
|      7 | 童装     |          4 |
|     19 | 美容美体 |         12 |
|     18 | 彩妆     |         12 |
|     13 | 护肤     |         12 |
|     15 | 护肤套装 |         13 |
|     40 | 防晒     |         13 |
|     39 | 卸妆     |         13 |
|     38 | 润唇膏   |         13 |
|     17 | 乳液面霜 |         13 |
|     16 | 面膜     |         13 |
|     14 | 化妆水   |         13 |
+--------+----------+------------+

1. 我们需要查询出"服装"分类下的所有子分类

?
1 2 3 4 5 6 7 8 9 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

+--------+------+------------+
| cat_id | name | parent_cid |
+--------+------+------------+
|      4 | 服装 |          0 |
|      5 | 女装 |          4 |
|      6 | 男装 |          4 |
|      7 | 童装 |          4 |
+--------+------+------------+

2. 查询出所有“美妆”分类下的所有子分类,并且分类名称带上上级分类的名称

?
1 2 3 4 5 6 7 8 9 10 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;

+--------+------------------------+------------+
| cat_id | name                   | parent_cid |
+--------+------------------------+------------+
|     12 | 美妆                   |          0 |
|     13 | 美妆>护肤              |         12 |
|     18 | 美妆>彩妆              |         12 |
|     19 | 美妆>美容美体          |         12 |
|     14 | 美妆>护肤>化妆水       |         13 |
|     15 | 美妆>护肤>护肤套装     |         13 |
|     16 | 美妆>护肤>面膜         |         13 |
|     17 | 美妆>护肤>乳液面霜     |         13 |
|     35 | 美妆>护肤>洁面         |         13 |
|     36 | 美妆>护肤>精华         |         13 |
|     37 | 美妆>护肤>眼霜         |         13 |
|     38 | 美妆>护肤>润唇膏       |         13 |
|     39 | 美妆>护肤>卸妆         |         13 |
|     40 | 美妆>护肤>防晒         |         13 |
+--------+------------------------+------------+

3. 查询分类的所有父级分类

根据第二个问题的sql做一下调整即可

?
1 2 3 4 5 6 7 8 9 10 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;

+--------+----------------+------------+
| cat_id | name           | parent_cid |
+--------+----------------+------------+
|     40 | 防晒           |         13 |
|     13 | 防晒>护肤      |         12 |
|     12 | 防晒>护肤>美妆 |          0 |
+--------+----------------+------------+

总结

到此这篇关于Mysql8.0递归查询的文章就介绍到这了,更多相关Mysql8.0递归查询内容请搜索服务器之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持服务器之家!

原文链接:https://juejin.cn/post/6991026506042441759

可以去百度分享获取分享代码输入这里。
声明

1.本站遵循行业规范,任何转载的稿件都会明确标注作者和来源;2.本站的原创文章,请转载时务必注明文章作者和来源,不尊重原创的行为我们将追究责任;3.作者投稿可能会经我们编辑修改或补充。

【腾讯云】云服务器产品特惠热卖中
搜索
标签列表
    关注我们

    了解等多精彩内容