SQL语句查询出一个父节点下的所有子节点

SQL语句查询出一个父节点下的所有子节点,第1张

假如你的表名字是tt,创建下面的存储过程,使用存储过程即可查询。

create proc querytree

@user varchar(100)

as

begin

declare @id int

declare @T_tmp table(id int,username varchar(100),parentid int)

insert into @T_tmp select from tt where username=@user

while(@@rowcount>0)

begin

insert into @T_tmp select from tt where parentid in (select id from @T_tmp) and id not in(select id from @T_tmp)

end

select from @T_tmp where username<>@user

end

直接查就行了,也可以试一下下面的:

select name,value,(select ParentValue from b) as ParentValue from a where OrgID = '001' ;

oracle判断是在Connect By子句中的Prior关键字内容,它可以让开发者通过比较由父结点和子结点表示的某列值的大小来判断当前节点是否处于一个指定的父级节点之下。例如,使用SELECT语句,我们可以检索出所有属于某一具体节点的子节点,而不用遍历每一层节点。

数据库中的该条记录是否有为父节点的字段

如果有的话就不用说了,只用判断该字段是否为null或者其他标识符,就知道是否为父节点了。

如果没有这个字段的话,你们的表结构中是否有存储子节点标识符的字段,还是一样判断是否为null或者其他标识符。

如果树是静态的话,直接写死的代码就可以了。

我们之前处理树的方式:表结构设计的时候有一个字段,用来存储父节点的主键字段,在数据处理的时候,从根节点开始,从表中搜索根节点的主键是否在其他对象中的父节点字段中存在,如果存在的话,OK,几个子节点就出来了,一次类推循环,树就出来了。

希望对你有帮助。

csdn上的例子 (仅供参考): private void treeView1_SelectedItemChanged(object sender, RoutedPropertyChangedEventArgs<object> e)

{

TreeViewItem item = (TreeViewItem)treeView1SelectedItem;

TreeViewItem it = new TreeViewItem();

if (!itemHasItems)

{

if (itemParent != null && (itemParent is TreeViewItem))

{

it = (TreeViewItem)itemParent;

MessageBoxShow(itHeaderToString() + (itemParent is TreeViewItem)ToString());

}

else

{

MessageBoxShow(itemHeaderToString());

}

}

}

活动树中节点的父子关系表示的含义是父亲节点被分解为了n个子节点。父结点在数据库管理中的数据模型中,早期阶段的层次模型和网状模型中,一个属性如果有上一级,则称这个上一级是它的父节点,如果没有上一级,则这个属性则无父节点。

 create table  ##tmp_users  (id int, username nvarchar(255), parentid int )

 declare @ID int

  select @ID=id from t_Users t where exists

 (select  from t_Users t2 where t2id=tparentid and t2username='user1') 

 exec AddSons @ID

 select  from ##tmp_users

 drop table ##tmp_users

 

 

 --存储

 create procedure AddSons @id int

 as

 if exists(select  from t_Users where parentid=@id)

 begin

  declare @tmp_ID int

  declare cur cursor for

   select id from t_Users where parentid=@id

  open cur

  fetch next from cur into @tmp_ID

  while @@FETCH_STATUS=0

  begin

   insert into ##tmp_users

   select  from t_Users t where id=@tmp_ID   

   if exists(select  from  t_Users where parentid=@tmp_ID)

   begin

    exec AddSons @tmp_ID

   end  

   fetch next from cur into @tmp_ID

  end

  close cur 

  DEALLOCATE cur  

 end 

 --递归调用,不知道是否想要这样

create table tmp

(

PARTNAME varchar(50),

QTY int,

CHILD_ID varchar(50),

PARENT_ID varchar(50)

)

GO

declare @name varchar(50)

set @name='AA'

insert into tmp

select distinct t2PARTNAME, t1QTYt2QTY,t2CHILD_ID,t2PARENT_ID from tree t1,tree t2 where t1PARENT_ID=t2CHILD_ID and t1PARTNAME=@name

while exists(select from tmp t1,tree t2 where t1PARENT_ID=t2CHILD_ID and t2PARTNAME not in(select PARTNAME from tmp))

begin

insert into tmp

select distinct t2PARTNAME, t1QTYt2QTY,t2CHILD_ID,t2PARENT_ID from tmp t1,tree t2 where t1PARENT_ID=t2CHILD_ID and t2PARTNAME not in(select PARTNAME from tmp)

end;

select PARTNAME,QTY,CHILD_ID from tmp where PARENT_ID=0

drop table tmp

以上就是关于SQL语句查询出一个父节点下的所有子节点全部的内容,包括:SQL语句查询出一个父节点下的所有子节点、SQL同时查询子、父节点的值、oracle判断是在哪个指定父级中等相关内容解答,如果想了解更多相关内容,可以关注我们,你们的支持是我们更新的动力!

欢迎分享,转载请注明来源:内存溢出

原文地址:https://54852.com/sjk/9846633.html

(0)
打赏 微信扫一扫微信扫一扫 支付宝扫一扫支付宝扫一扫
上一篇 2023-05-02
下一篇2023-05-02

发表评论

登录后才能评论

评论列表(0条)

    保存