激情久久久_欧美视频区_成人av免费_不卡视频一二三区_欧美精品在欧美一区二区少妇_欧美一区二区三区的

服務器之家:專注于服務器技術及軟件下載分享
分類導航

Mysql|Sql Server|Oracle|Redis|MongoDB|PostgreSQL|Sqlite|DB2|mariadb|Access|數據庫技術|

服務器之家 - 數據庫 - Mysql - Mysql8.0遞歸查詢的簡單用法示例

Mysql8.0遞歸查詢的簡單用法示例

2021-09-13 17:44阿豪_mike Mysql

在項目中會遇到同一個表中保存著父子關系的數據,最常見的就是處理樹形結構資源,這篇文章主要給大家介紹了關于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. 我們需要查詢出"服裝"分類下的所有子分類

?
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

延伸 · 閱讀

精彩推薦
主站蜘蛛池模板: 欧美精品免费一区二区三区 | 成年人视频在线免费观看 | 久久99精品久久久久久青青日本 | 免费一级特黄做受大片 | 久久国产精品久久精品国产演员表 | www.99久久久 | 北京一级毛片 | 精品国产高清一区二区三区 | 毛片在线看免费 | 免费看h网站 | 国产a级久久 | www视频免费在线观看 | xxxx18韩国护士hd老师 | 国产精品久久久久久久久久久久久久久久 | 日本成人一区二区 | 日本一区二区三区精品 | 亚洲精品一区国产精品丝瓜 | 狠狠操精品视频 | 1000部精品久久久久久久久 | 久久国产精品99久久人人澡 | 免费啪视频在线观看 | 啪啪激情 | 草操视频 | 久草在线视频看看 | 韩国精品久久久 | 免费看a级片 | 欧美精品一区二区三区久久久 | 成人毛片视频在线观看 | 免费一级高清毛片 | 97中文 | 72pao成人国产永久免费视频 | 日日天日日夜日日摸 | 亚洲一区二区三区精品在线观看 | 日韩大片在线永久观看视频网站免费 | 超久久 | 免费国产自久久久久三四区久久 | 久久国产精品久久久久久 | 国产乱淫a∨片免费观看 | 久久久久久久久久久国产精品 | 精品不卡| 91www成人久久|