<small id='dSCSL'></small><noframes id='dSCSL'>

<tfoot id='dSCSL'></tfoot>
  • <i id='dSCSL'><tr id='dSCSL'><dt id='dSCSL'><q id='dSCSL'><span id='dSCSL'><b id='dSCSL'><form id='dSCSL'><ins id='dSCSL'></ins><ul id='dSCSL'></ul><sub id='dSCSL'></sub></form><legend id='dSCSL'></legend><bdo id='dSCSL'><pre id='dSCSL'><center id='dSCSL'></center></pre></bdo></b><th id='dSCSL'></th></span></q></dt></tr></i><div id='dSCSL'><tfoot id='dSCSL'></tfoot><dl id='dSCSL'><fieldset id='dSCSL'></fieldset></dl></div>
    • <bdo id='dSCSL'></bdo><ul id='dSCSL'></ul>

  • <legend id='dSCSL'><style id='dSCSL'><dir id='dSCSL'><q id='dSCSL'></q></dir></style></legend>

        为孩子获取所有父母

        时间:2023-10-09
      1. <small id='ImYE7'></small><noframes id='ImYE7'>

          <bdo id='ImYE7'></bdo><ul id='ImYE7'></ul>
          <tfoot id='ImYE7'></tfoot>
            <legend id='ImYE7'><style id='ImYE7'><dir id='ImYE7'><q id='ImYE7'></q></dir></style></legend>

                <tbody id='ImYE7'></tbody>

              <i id='ImYE7'><tr id='ImYE7'><dt id='ImYE7'><q id='ImYE7'><span id='ImYE7'><b id='ImYE7'><form id='ImYE7'><ins id='ImYE7'></ins><ul id='ImYE7'></ul><sub id='ImYE7'></sub></form><legend id='ImYE7'></legend><bdo id='ImYE7'><pre id='ImYE7'><center id='ImYE7'></center></pre></bdo></b><th id='ImYE7'></th></span></q></dt></tr></i><div id='ImYE7'><tfoot id='ImYE7'></tfoot><dl id='ImYE7'><fieldset id='ImYE7'></fieldset></dl></div>

                  本文介绍了为孩子获取所有父母的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  我想检索一个 id 的 parentid,如果该 parentid 有一个 parent,则再次检索它,依此类推.一种层次结构表.

                  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
                  1-----1
                  5-----1
                  47894--5
                  47897--47894
                  

                  我是 sql server 的新手并尝试过一些查询,例如:

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

                  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)

                  推荐答案

                  试试这个让孩子的所有父母都知道

                  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
                  

                  点击这里查看结果

                  如果你想插入一个表变量,你可以这样做:

                  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
                  

                  点击这里查看结果

                  这篇关于为孩子获取所有父母的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  上一篇:mysql 层次结构自加入,检索所有子类别 下一篇:带有关系 innoDB 的递归 MySQL 查询

                  相关文章

                • <tfoot id='sT9eB'></tfoot>

                  • <bdo id='sT9eB'></bdo><ul id='sT9eB'></ul>
                • <i id='sT9eB'><tr id='sT9eB'><dt id='sT9eB'><q id='sT9eB'><span id='sT9eB'><b id='sT9eB'><form id='sT9eB'><ins id='sT9eB'></ins><ul id='sT9eB'></ul><sub id='sT9eB'></sub></form><legend id='sT9eB'></legend><bdo id='sT9eB'><pre id='sT9eB'><center id='sT9eB'></center></pre></bdo></b><th id='sT9eB'></th></span></q></dt></tr></i><div id='sT9eB'><tfoot id='sT9eB'></tfoot><dl id='sT9eB'><fieldset id='sT9eB'></fieldset></dl></div>

                    <legend id='sT9eB'><style id='sT9eB'><dir id='sT9eB'><q id='sT9eB'></q></dir></style></legend>
                  1. <small id='sT9eB'></small><noframes id='sT9eB'>