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

作者:阿豪_mike 时间:2024-01-22 16:06:42 

前言

本文使用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 |
+--------+----------------+------------+

总结

来源:https://juejin.cn/post/6991026506042441759

标签:mysql8.0,递归,查询
0
投稿

猜你喜欢

  • vue项目配置element-ui容易遇到的坑及解决

    2024-04-29 13:10:25
  • Python大批量搜索引擎图像爬虫工具详解

    2021-09-15 15:30:02
  • JavaScript forEach()遍历函数使用及介绍

    2024-05-11 09:07:07
  • 利用scikitlearn画ROC曲线实例

    2023-11-05 16:00:33
  • python中的turtle库函数简单使用教程

    2022-08-06 23:22:18
  • golang 用msgpack高效序列化的案例

    2024-04-26 17:32:52
  • BeautifulSoup中find和find_all的使用详解

    2023-11-08 21:00:22
  • vue面试之new Vue的时候到底做了什么

    2024-06-05 15:31:27
  • c#连接sqlserver数据库、插入数据、从数据库获取时间示例

    2024-01-16 02:13:53
  • Yahoo!网站性能最佳体验的34条黄金守则——图片、Coockie与移动应用

    2008-05-29 13:44:00
  • Vue keepAlive 数据缓存工具实现返回上一个页面浏览的位置

    2024-04-30 10:28:47
  • Python操作Excel神器openpyxl使用教程(超详细!)

    2021-02-28 21:58:35
  • python网络爬虫基于selenium爬取斗鱼直播信息

    2023-03-05 09:33:19
  • 利用Python+PyQt5实现简易浏览器的实战记录

    2022-10-30 23:16:26
  • T-SQL中使用正则表达式函数

    2024-01-27 13:36:58
  • JavaScript实现alert弹框效果

    2024-05-10 10:57:40
  • pycharm打包py项目为.exe可执行文件的两种方式

    2022-03-26 11:51:51
  • 开展全面的网站评估

    2007-09-27 19:21:00
  • Python使用微信SDK实现的微信支付功能示例

    2022-09-22 00:19:41
  • Python imutils 填充图片周边为黑色的实现

    2021-04-13 04:06:32
  • asp之家 网络编程 m.aspxhome.com