让所有父母为孩子

时间:2021-06-08 21:18:12

I want to retrieve the parentid of an id, if that parentid has a parent again retrieve it, and so on. Kind of hierarchy table.

我想检索一个id的parentid,如果那个parentid有一个父母再次检索它,依此类推。一种层次表。

id----parentid
1-----1
5-----1
47894--5
47897--47894

am new to sql server and tried, some queries like:

我是sql server的新手并试过,有些查询如下:

with name_tree as 
(
   select id, parentid
   from Users
   where id = 47897 -- this is the starting point you want in your recursion
   union all
   select c.id, c.parentid
   from users c
   join name_tree p on p.id = c.parentid  -- this is the recursion
) 
select *
from name_tree;

It is giving me only one row. and also I want to insert these records into a temporary table variable. How can I do this. thanks in advance. sorry for asking the simple question(though not to me)

它只给我一排。我还想将这些记录插入临时表变量中。我怎样才能做到这一点。提前致谢。抱歉问这个简单的问题(虽然不是我)

3 个解决方案

#1


22  

Try this to get all parents of a child

试着这个让所有孩子的父母

;with name_tree as 
(
   select id, parentid
   from Users
   where id = 47897 -- this is the starting point you want in your recursion
   union all
   select C.id, C.parentid
   from Users c
   join name_tree p on C.id = P.parentid  -- this is the recursion
   -- Since your parent id is not NULL the recursion will happen continously.
   -- For that we apply the condition C.id<>C.parentid 
    AND C.id<>C.parentid 
) 
-- Here you can insert directly to a temp table without CREATE TABLE synthax
select *
INTO #TEMP
from name_tree
OPTION (MAXRECURSION 0)

SELECT * FROM #TEMP

Click here to view result

点击这里查看结果

EDIT :

编辑:

If you want to insert into a table variable, you can do something like:

如果要插入表变量,可以执行以下操作:

-- Declare table varialbe
Declare @TABLEVAR table (id int ,parentid int)


;with name_tree as 
(
   select id, parentid
   from #Users
   where id = 47897 -- this is the starting point you want in your recursion
   union all
   select C.id, C.parentid
   from #Users c
   join name_tree p on C.id = P.parentid  -- this is the recursion
   -- Since your parent id is not NULL the recursion will happen continously.
   -- For that we apply the condition C.id<>C.parentid 
    AND C.id<>C.parentid 
) 
-- Here you can insert directly to table variable
INSERT INTO @TABLEVAR
select *
from name_tree
OPTION (MAXRECURSION 0)

SELECT * FROM @TABLEVAR

Click here to view result

点击这里查看结果

#2


0  

Your query is doing recursion but in opposite direction. So if you change starting point to:

您的查询正在进行递归,但方向相反。因此,如果您将起点更改为:

where id = 1

then you will have user 1 and all his successors

那么你将拥有用户1和他所有的继任者

#3


0  

you didn't mention the desired output and input. However you can try like this,

你没有提到所需的输出和输入。不过你可以这样试试,

Declare @t table (id int ,parentid int)
insert into @t
select 1,1 union all
select 5,1 union all
select 47894,5 union all
select 47897,47894 

;With CTE as
(
select * from @t where id=1
union all
Select a.* from @t a inner join cte b
 on b.id=a.parentid and
a.id<>b.id
)
select * from cte

#1


22  

Try this to get all parents of a child

试着这个让所有孩子的父母

;with name_tree as 
(
   select id, parentid
   from Users
   where id = 47897 -- this is the starting point you want in your recursion
   union all
   select C.id, C.parentid
   from Users c
   join name_tree p on C.id = P.parentid  -- this is the recursion
   -- Since your parent id is not NULL the recursion will happen continously.
   -- For that we apply the condition C.id<>C.parentid 
    AND C.id<>C.parentid 
) 
-- Here you can insert directly to a temp table without CREATE TABLE synthax
select *
INTO #TEMP
from name_tree
OPTION (MAXRECURSION 0)

SELECT * FROM #TEMP

Click here to view result

点击这里查看结果

EDIT :

编辑:

If you want to insert into a table variable, you can do something like:

如果要插入表变量,可以执行以下操作:

-- Declare table varialbe
Declare @TABLEVAR table (id int ,parentid int)


;with name_tree as 
(
   select id, parentid
   from #Users
   where id = 47897 -- this is the starting point you want in your recursion
   union all
   select C.id, C.parentid
   from #Users c
   join name_tree p on C.id = P.parentid  -- this is the recursion
   -- Since your parent id is not NULL the recursion will happen continously.
   -- For that we apply the condition C.id<>C.parentid 
    AND C.id<>C.parentid 
) 
-- Here you can insert directly to table variable
INSERT INTO @TABLEVAR
select *
from name_tree
OPTION (MAXRECURSION 0)

SELECT * FROM @TABLEVAR

Click here to view result

点击这里查看结果

#2


0  

Your query is doing recursion but in opposite direction. So if you change starting point to:

您的查询正在进行递归,但方向相反。因此,如果您将起点更改为:

where id = 1

then you will have user 1 and all his successors

那么你将拥有用户1和他所有的继任者

#3


0  

you didn't mention the desired output and input. However you can try like this,

你没有提到所需的输出和输入。不过你可以这样试试,

Declare @t table (id int ,parentid int)
insert into @t
select 1,1 union all
select 5,1 union all
select 47894,5 union all
select 47897,47894 

;With CTE as
(
select * from @t where id=1
union all
Select a.* from @t a inner join cte b
 on b.id=a.parentid and
a.id<>b.id
)
select * from cte