--由父项递归下级 |
with cte(id,parentid,text) |
as |
(--父项 |
select id,parentid,text from treeview where parentid = 450 |
union all |
--递归结果集中的下级 |
select t.id,t.parentid,t.text from treeview as t |
inner join cte as c on t.parentid = c.id |
) |
select id,parentid,text from cte |
|
--------------------- |
|
--由子级递归父项 |
with cte(id,parentid,text) |
as |
(--下级父项 |
select id,parentid,text from treeview where id = 450 |
union all |
--递归结果集中的父项 |
select t.id,t.parentid,t.text from treeview as t |
inner join cte as c on t.id = c.parentid |
) |
select id,parentid,text from cte |
本文介绍了一种使用CTE(公共表表达式)进行递归查询的方法,包括从父项递归查询所有下级节点及从子项递归查询所有上级节点的过程。这种方法适用于树形结构的数据表,如组织架构、文件目录等。
2189

被折叠的 条评论
为什么被折叠?



