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

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

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

        mysql递归(树)父子类

        时间:2023-10-09
          <tbody id='Yld8V'></tbody>
      2. <legend id='Yld8V'><style id='Yld8V'><dir id='Yld8V'><q id='Yld8V'></q></dir></style></legend>
      3. <small id='Yld8V'></small><noframes id='Yld8V'>

                <bdo id='Yld8V'></bdo><ul id='Yld8V'></ul>

                <tfoot id='Yld8V'></tfoot>
                <i id='Yld8V'><tr id='Yld8V'><dt id='Yld8V'><q id='Yld8V'><span id='Yld8V'><b id='Yld8V'><form id='Yld8V'><ins id='Yld8V'></ins><ul id='Yld8V'></ul><sub id='Yld8V'></sub></form><legend id='Yld8V'></legend><bdo id='Yld8V'><pre id='Yld8V'><center id='Yld8V'></center></pre></bdo></b><th id='Yld8V'></th></span></q></dt></tr></i><div id='Yld8V'><tfoot id='Yld8V'></tfoot><dl id='Yld8V'><fieldset id='Yld8V'></fieldset></dl></div>
                  本文介绍了mysql递归(树)父子类的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  我是 mysql 新手.这是我的桌子:

                  I am new in mysql. This is my table:

                  类别表:

                  id | name        | prent
                  ----------------------------
                  1  |  os         | null
                  2  | linux       | 1
                  3  | ubuntu      | 2
                  4  | xubuntu     | 3
                  5  | lubuntu     | 3
                  6  | zubuntu     | 3
                  7  | zubuntu 2   | 6
                  8  | suse        | 2
                  9  | fedora      | 2
                  10 | windowse    | 1
                  11 | windowse xp | 10
                  12 | windowse 7  | 10
                  13 | windowse 8  | 10
                  14 | food        | null
                  15 | dance       | null
                  

                  每个类别都有一个父类别,我想准备它们以显示在下拉菜单中.

                  Each category has a parent and I want to prepare them to show in a drop-down menu.

                  这就是我想要的:

                  id | name          | depth
                  ----------------------------
                  1  | os            | 0
                  2  | -linux        | 1
                  3  | --ubuntu      | 2
                  4  | ---xubuntu    | 3
                  5  | ---lubuntu    | 3
                  6  | ---zubuntu    | 3
                  7  | ----zubuntu 2 | 4
                  8  | --suse        | 2
                  9  | --fedora      | 2
                  10 | -windows      | 1
                  11 | --windows  xp | 2
                  12 | --windows  7  | 2
                  13 | --windows  8  | 2
                  14 | food          | 0
                  15 | dance         | 0
                  

                  在这里,类别没有顺序,我的代码必须为远离父母的子类别提供顺序.根据每个类别的父级的深度提供名称前的缩进.每个类别的子项数量没有限制,但类别总数不会超过100个.

                  Here, categories are not in order and my code has to provide order for children categories far from their parents. Indentation before name is provided based on depth of parents of each category. There is no limit for number of children of each category however the total number of categories will not exceed 100.

                  是否有任何查询会给出这样的结果?我更喜欢可以在 PHP 框架中以 活动记录 形式运行的查询.>

                  Is there any query that gives such result? I prefer a query that can be run in form of active record in a PHP framework.

                  推荐答案

                  这个 Thread 引导我.感谢@RolandoMySQLDBA

                  This Thread led me. Thanks to @RolandoMySQLDBA

                  DELIMITER $$
                  DROP FUNCTION IF EXISTS `GetAncestry` $$
                  CREATE FUNCTION `GetAncestry` (GivenID INT) RETURNS VARCHAR(1024)
                  DETERMINISTIC
                  BEGIN
                      DECLARE rv VARCHAR(1024);
                      DECLARE cm CHAR(1);
                      DECLARE ch INT;
                  
                      SET rv = '';
                      SET cm = '';
                      SET ch = GivenID;
                      WHILE ch > 0 DO
                          SELECT IFNULL(`prent`,-1) INTO ch FROM
                          (SELECT `prent` FROM Table1 WHERE id = ch) A;
                          IF ch > 0 THEN
                              SET rv = CONCAT(rv,cm,ch);
                              SET cm = ',';
                          END IF;
                      END WHILE;
                      RETURN rv;
                  
                  END $$
                  DELIMITER ;
                  

                  一个有效的fiddle这里.

                  SELECT id,GetAncestry(id) as parents from Table1 where id = 7;
                  
                  ID  PARENTS
                  7   6,3,2,1
                  

                  这篇关于mysql递归(树)父子类的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

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

                    <legend id='iwnCj'><style id='iwnCj'><dir id='iwnCj'><q id='iwnCj'></q></dir></style></legend>
                    • <bdo id='iwnCj'></bdo><ul id='iwnCj'></ul>

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

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