欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

Mysql8.0遞歸查詢(xún)的簡(jiǎn)單用法示例

 更新時(shí)間:2021年08月03日 14:48:01   作者:阿豪_mike  
在項(xiàng)目中會(huì)遇到同一個(gè)表中保存著父子關(guān)系的數(shù)據(jù),最常見(jiàn)的就是處理樹(shù)形結(jié)構(gòu)資源,這篇文章主要給大家介紹了關(guān)于Mysql8.0遞歸查詢(xún)的簡(jiǎn)單用法,需要的朋友可以參考下

前言

本文使用Mysql8.0的特新實(shí)現(xiàn)遞歸查詢(xún),文中給出了詳細(xì)的實(shí)例代碼,下面話不多說(shuō)了,來(lái)一起看看詳細(xì)的介紹吧

Mysql8.0遞歸查詢(xún)用法

表數(shù)據(jù)如下

+--------+----------+------------+
| cat_id | name     | parent_cid |
+--------+----------+------------+
|     12 | 美妝     |          0 |
|      4 | 服裝     |          0 |
|      5 | 女裝     |          4 |
|      6 | 男裝     |          4 |
|      7 | 童裝     |          4 |
|     19 | 美容美體 |         12 |
|     18 | 彩妝     |         12 |
|     13 | 護(hù)膚     |         12 |
|     15 | 護(hù)膚套裝 |         13 |
|     40 | 防曬     |         13 |
|     39 | 卸妝     |         13 |
|     38 | 潤(rùn)唇膏   |         13 |
|     17 | 乳液面霜 |         13 |
|     16 | 面膜     |         13 |
|     14 | 化妝水   |         13 |
+--------+----------+------------+

1. 我們需要查詢(xún)出"服裝"分類(lèi)下的所有子分類(lèi)

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. 查詢(xún)出所有“美妝”分類(lèi)下的所有子分類(lèi),并且分類(lèi)名稱(chēng)帶上上級(jí)分類(lèi)的名稱(chēng)

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 | 美妝>護(hù)膚              |         12 |
|     18 | 美妝>彩妝              |         12 |
|     19 | 美妝>美容美體          |         12 |
|     14 | 美妝>護(hù)膚>化妝水       |         13 |
|     15 | 美妝>護(hù)膚>護(hù)膚套裝     |         13 |
|     16 | 美妝>護(hù)膚>面膜         |         13 |
|     17 | 美妝>護(hù)膚>乳液面霜     |         13 |
|     35 | 美妝>護(hù)膚>潔面         |         13 |
|     36 | 美妝>護(hù)膚>精華         |         13 |
|     37 | 美妝>護(hù)膚>眼霜         |         13 |
|     38 | 美妝>護(hù)膚>潤(rùn)唇膏       |         13 |
|     39 | 美妝>護(hù)膚>卸妝         |         13 |
|     40 | 美妝>護(hù)膚>防曬         |         13 |
+--------+------------------------+------------+

3. 查詢(xún)分類(lèi)的所有父級(jí)分類(lèi)

根據(jù)第二個(gè)問(wèn)題的sql做一下調(diào)整即可

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 | 防曬>護(hù)膚      |         12 |
|     12 | 防曬>護(hù)膚>美妝 |          0 |
+--------+----------------+------------+

總結(jié)

到此這篇關(guān)于Mysql8.0遞歸查詢(xún)的文章就介紹到這了,更多相關(guān)Mysql8.0遞歸查詢(xún)內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

最新評(píng)論