ICode9

精准搜索请尝试: 精确搜索
首页 > 数据库> 文章详细

mysql递归查询-案例(2)

2022-09-17 00:05:23  阅读:326  来源: 互联网

标签:13 parent 递归 美妆 查询 mysql cat type id


表数据如下

+--------+----------+------------+
| 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 |
+--------+----------------+------------+

 

标签:13,parent,递归,美妆,查询,mysql,cat,type,id
来源: https://www.cnblogs.com/xiaoyongdata/p/16701683.html

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有