SQL---查询树中某个节点及其所有子节点

with f as
(
select * from tab where id=1
union all
select a.* from tab as a inner join f as b on a.pid=b.id
)

select * from f

  

时间: 2024-08-25 18:51:26

SQL---查询树中某个节点及其所有子节点的相关文章

[SQL]T-Sql 递归查询(给定节点查所有父节点、所有子节点的方法)

-- 查找所有父节点with tab as( select Type_Id,ParentId,Type_Name from Sys_ParamType_V2_0 where Type_Id=316--子节点 union all select b.Type_Id,b.ParentId,b.Type_Name  from  tab a,--子节点数据集  Sys_ParamType_V2_0 b  --父节点数据集 where a.ParentId=b.Type_Id  --子节点数据集.paren

T-Sql 递归查询(给定节点查所有父节点、所有子节点的方法)

-- 查找所有父节点with tab as( select Type_Id,ParentId,Type_Name from Sys_ParamType_V2_0 where Type_Id=316--子节点 union all select b.Type_Id,b.ParentId,b.Type_Name  from  tab a,--子节点数据集  Sys_ParamType_V2_0 b  --父节点数据集 where a.ParentId=b.Type_Id  --子节点数据集.paren

With As实现查找表中指定节点的所有子节点

要实现的树如图所示,要求能根据“节点1”获取其所有子节点 数据库表设计如下: 在这里用到With As WITH tb AS { SELECT* FROM node WHERE nodeId=1 UNION ALL SELECT node.nodeId,node.nodeName,node.parentId FROM node,tb WHERE node.parentId=tb.nodeId } SELECT * FROM tb

SQL Server 查询树结构的表,查询一个节点的所有子节点

1 ;with cte as 2 ( 3 select * from Associator where No = 'mc1007' 4 union all 5 select air.* from Associator as air inner join cte on air.ParentNo = cte.No 6 ) 7 select * from cte;

SQL 语句递归查询 With AS 查找所有子节点

create table #EnterPrise(  Department nvarchar(50),--部门名称  ParentDept nvarchar(50),--上级部门  DepartManage nvarchar(30)--部门经理) insert into #EnterPrise select '技术部','总经办','Tom'insert into #EnterPrise select '商务部','总经办','Jeffry'insert into #EnterPrise sel

sqlserver查询指定树形结构的所有子节点

用标准sql的with实现递归查询(sql2005以上肯定支持,sql2000不清楚是否支持): with subqry(id,name,pid) as ( select id,name,pid from test1 where id = 5 --指定id union all select test1.id,test1.name,test1.pid from test1,subqry where test1.pid = subqry.id)select* from subqry;

mysql 递归查找菜单节点的所有子节点

SELECT    idFROM    (        SELECT            t1.id, IF (            find_in_set(parent_id, @pids) > 0, @pids := concat(@pids, ',', id), 0) AS ischildFROM    (        SELECT            id,            parent_id        FROM            rde_menu t      

SQL 递归找查所有子节点及所有父节

在SQL的树型结构中,很多时候,知道某一节点的值,需要查找该节点的所有子节点(包括多级)的功能,这时就需要用到如下的用户自定义函数. 表结构如下: ID int Dep_Type int Dep_Code varchar(50) Dep_Name varchar(50) Dep_Dian int Dep_FathID int Dep_Opera varchar(50) Dep_Status int Dep_AddTime datetime 用户自定义函数如下: create function f

最佳数据库无限分级快速查找所有子节点的方法

场景我们基本设计的表是这样的 temp表 id, name, parent_id 当我们查某个节点的所有子节点的时候,我们需要递归查询 id = 4 select * from temp where parent_id = 4 ids = [5,9,25] select * from temp where parent_id in [5,9,25] 那么这种方法在层数达到一定层的时候势必带来性能问题,因为需要多次查询数据库,就算写存储过程,性能也是十分低下的. 快速查询方法如下: 改造表,添加c