温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

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

发布时间:2021-08-03 17:19:36 来源:亿速云 阅读:133 作者:chen 栏目:开发技术

这篇文章主要介绍“Mysql8.0递归查询的简单用法”,在日常操作中,相信很多人在Mysql8.0递归查询的简单用法问题上存在疑惑,小编查阅了各式资料,整理出简单好用的操作方法,希望对大家解答”Mysql8.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. 我们需要查询出"服装"分类下的所有子分类

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. 查询出所有“美妆”分类下的所有子分类,并且分类名称带上上级分类的名称

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做一下调整即可

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递归查询的简单用法”的学习就结束了,希望能够解决大家的疑惑。理论与实践的搭配能更好的帮助大家学习,快去试试吧!若想继续学习更多相关知识,请继续关注亿速云网站,小编会继续努力为大家带来更多实用的文章!

向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI