MySql8使用WITH RECURSIVE进行递归查询下级节点数据

#查询id=62的所有子节点
WITH RECURSIVE temp AS (
		SELECT * FROM tbsys_office o WHERE o.id=62
		UNION ALL
		SELECT o.* FROM tbsys_office o,temp t WHERE t.id=o.parent_id
) SELECT * FROM temp;
#查询id=80的所有父节点
WITH RECURSIVE temp AS (
		SELECT * FROM tbsys_office o WHERE o.id=80
		UNION ALL
		SELECT o.* FROM tbsys_office o,temp t WHERE t.parent_id=o.id
) SELECT * FROM temp;

原文链接:数据库技术:MySql8 WITH RECURSIVE递归查询父子集的方法

你可能感兴趣的:(mysql,数据库)