PHP/MYSQL 父子关系

发布于 2024-11-05 12:32:30 字数 485 浏览 0 评论 0原文

我有一个像这样的表:

  • id
  • name
  • Parent_id

然后我想根据它们的 id 选择某些行,所以像这样:

SELECT * 
  FROM TABLE 
 WHERE id IN ('1', '5', '8', '9', '35')  

我想从这个查询中也显示父/子关系,例如:

id   parent  
-----------
1    0  
5    1  
8    0  
9    8  
35   9  

所以最终的输出将看起来像这样:

1  
--5  

8   
--9  
 ----35  

我是否在 mysql 之外执行此操作,我尝试过使用数组,但无法弄清楚,或者
我是在MYSQL里面做的吗,我也不知道怎么做。

I have a table like this:

  • id
  • name
  • parent_id

I then want to select certain rows based on their id, so something like this:

SELECT * 
  FROM TABLE 
 WHERE id IN ('1', '5', '8', '9', '35')  

I want to, from this query, also show the parent/child relationship, like:

id   parent  
-----------
1    0  
5    1  
8    0  
9    8  
35   9  

So the final output would look something like this:

1  
--5  

8   
--9  
 ----35  

Do I do this outside of mysql, i have tried using arrays, but can't figure it out, or
Do I do it inside MYSQL, which i don't know how to do that either.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

木森分化 2024-11-12 12:32:30

这是我能得到的,看起来效果很好。

PS-抱歉格式问题,无法弄清楚:( (已修复?)

  1. 我从 MYSQL 获取我的parent_id 和 id 并将其放入数组中键是 id,值是父级,因此在 mysql 的 while 循环中,如下所示: $testarray[$id] = $parent_id;
  2. 然后运行它通过下面的函数,它会按照我需要的方式进行排序。

    函数retrieveSubTree($parent, $myarray) {
        $tempArray = $myarray;
        $数组=数组();           
        //现在我们有了顶级父级,让我们将其子级放入一个数组中,是的!
        while ($child = array_search($parent, $tempArray)) {
            取消设置($tempArray[$child]);
            //现在让这些家伙成为孩子
            if (in_array($child, $tempArray)) {
                $array[$child] =retrieveSubTree($child, $tempArray);
            } 别的 {
                $array[$child] = true;
            }
        }//结束同时
        返回(!空($数组))? $数组:假;
    }
    
    函数retrieveTree($myarray) {
        $数组=数组();
        $计数器= 0;
        foreach ($myarray as $key => $value) {
            $孩子= $钥匙;
            $父= $值;
            //如果这个孩子是其他人的父母
            if (in_array($child, $myarray) && $parent != '0') {
                while ($myarray[$parent] != '' && $myarray[$parent] != '0') {
                    $newparent = $myarray[$parent];
                    $parent = $newparent;
                }
                if (!array_key_exists($parent, $array)) {
                    $array[$parent] =retrieveSubTree($parent, $myarray);
                }
            } 别的 {
                //现在确保它们不会显示为某个子项
                if (!array_key_exists($parent, $myarray)) {
                    //看看它是否是任何人的父母
                    if (in_array($child, $myarray)) {
                        $array[$child] =retrieveSubTree($child, $myarray);
                    } 别的 {
                        $array[$child] = true;
                    }
                }//如果数组键结束
            }//结束数组中的首字母
        }//结束foreach
        return (!empty($array) ? $array : false);
    } 
    
    $测试=数组(
        '1'=>'15',
        '2'=>'1',
        '3'=>'1',
        '4'=>'0',
        '5'=>'0',
        '6'=>'4',
        '7'=>'6',
        '8'=>'7',
        '9'=>'2',
        '10'=>'9'
    );
    
    print_r(retrieveTree($test)); 
    

Here is what I was able to come with which seems to be working great.

PS-Sorry about the formatting, can't figure it out :( (fixed?)

  1. I grab my parent_id and id from MYSQL and put it into an arraly where the array keys are the id's and the values are the parents, so with in the while loop for mysql, something like this: $testarray[$id] = $parent_id;
  2. Then I run it through the functions below, and it orders it just how I need it.

    function retrieveSubTree($parent, $myarray) {
        $tempArray = $myarray;
        $array = array();           
        //now we have our top level parent, lets put its children into an array, yea!
        while ($child = array_search($parent, $tempArray)) {
            unset($tempArray[$child]);
            //now lets get all this guys children
            if (in_array($child, $tempArray)) {
                $array[$child] = retrieveSubTree($child, $tempArray);
            } else {
                $array[$child] = true;
            }
        }//end while
        return (!empty($array)) ? $array : false;
    }
    
    function retrieveTree($myarray) {
        $array = array();
        $counter = 0;
        foreach ($myarray as $key => $value) {
            $child = $key;
            $parent = $value;
            //if this child is a parent of somebody else
            if (in_array($child, $myarray) && $parent != '0') {
                while ($myarray[$parent] != '' && $myarray[$parent] != '0') {
                    $newparent = $myarray[$parent];
                    $parent = $newparent;
                }
                if (!array_key_exists($parent, $array)) {
                    $array[$parent] = retrieveSubTree($parent, $myarray);
                }
            } else {
                //now make sure they don't appear as some child
                if (!array_key_exists($parent, $myarray)) {
                    //see if it is a parent of anybody
                    if (in_array($child, $myarray)) {
                        $array[$child] = retrieveSubTree($child, $myarray);
                    } else {
                        $array[$child] = true;
                    }
                }//end if array key
            }//end initial in array
        }//end foreach
        return (!empty($array) ? $array : false);
    } 
    
    $test = array(
        '1'=>'15',
        '2'=>'1',
        '3'=>'1',
        '4'=>'0',
        '5'=>'0',
        '6'=>'4',
        '7'=>'6',
        '8'=>'7',
        '9'=>'2',
        '10'=>'9'
    );
    
    print_r(retrieveTree($test)); 
    
居里长安 2024-11-12 12:32:30

在不改变表结构的情况下,这需要递归,而 MySQL 不支持递归。你必须在其他地方做这件事。您可以在 PHP 中编写递归函数来使用,例如 广度优先搜索构建你的数组。在这里,您似乎使用 0 的 parent_id 来表示顶级对象。您可以搜索结果,并将父级为零的每个对象添加到数组中,这将为您提供一个包含 1 和 8 的数组。然后您可以递归:找到父级为 1 的所有结果,并将其添加为子数组为 1;然后找到父级为 8 的所有结果,并将它们添加为 8 的子数组。对每个级别继续执行此操作,直到用完结果为止。

正如其他发帖者指出的那样,如果您可以更改表结构,则可以在 MySQL 中本地执行此操作。

Without changing your table structure, this requires recursion, which MySQL does not support. You'll have to do it elsewhere. You can write a recursive function in PHP to use, for example, breadth-first search to build your array. Here it looks like you are using parent_id of 0 to denote a top-level object. You can search over your results, and add to your array every object whose parent is zero, which will give you an array with 1 and 8. Then you can recurse: find all the results with a parent of 1, and add that as a subarray to 1; then find all the results with a parent of 8 and add those as a subarray of 8. Continue doing this for each level until you've run out of results.

As other posters pointed out, you can do this natively in MySQL if you can change the table structure.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文