mysql 如何获取其及其所有子孙的数据或id (一)

/* 通过组织机构i数据获取其所有子孙数据*/ 
WITH RECURSIVE cte AS
(SELECT * FROM organization WHERE organization_id = 1
UNION ALL
SELECT organization.* FROM organization INNER JOIN cte ON organization.parent_id = cte.organization_id)
SELECT * FROM cte;

  

/* 通过组织机构id获取其所有子孙id*/ 
WITH RECURSIVE cte AS
(SELECT organization_id FROM organization WHERE organization_id = 1
UNION ALL
SELECT organization.organization_id FROM organization INNER JOIN cte ON organization.parent_id = cte.organization_id)
SELECT * FROM cte;

  

WITH RECURSIVE cte 该语法只能在mysql 8.0才能使用,那之前的版本怎么做的呢?存储函数,点击下面的链接查看。

mysql 如何获取其及其所有子孙的数据或id (一)

 

 

想看更多精彩内容,可以关注我的CSDN

 

我的CSDN

 

posted @ 2021-04-08 08:36  Yblue  阅读(472)  评论(0编辑  收藏  举报