数据库
首页 > 数据库> > mysql 如何获取其及其所有子孙的数据或id

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;

  

标签:JOIN,子孙,cte,mysql,organization,WHERE,id,SELECT
来源: https://www.cnblogs.com/yblue/p/14629304.html