Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/257.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/mysql/67.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在mysql或php中获取父级的所有子代?_Php_Mysql_Parent - Fatal编程技术网

在mysql或php中获取父级的所有子代?

在mysql或php中获取父级的所有子代?,php,mysql,parent,Php,Mysql,Parent,希望能得到一些帮助。假设我有一个像这样的mysql表 ID | Content | Parent ID | 2 | A | 0 3 | A | 2 4 | A | 3 5 | A | 4 6 | A | 10 7 | A | 0 8 | A | 7 9 | A | 8 10 | A | 9 我想知道ID 2的所有后代 3是孩子。孙子孙女可以是任何父ID为3的帖子。等等

希望能得到一些帮助。假设我有一个像这样的mysql表

ID | Content | Parent ID |
2  | A       | 0
3  | A       | 2
4  | A       | 3  
5  | A       | 4
6  | A       | 10
7  | A       | 0
8  | A       | 7
9  | A       | 8
10 | A       | 9
我想知道ID 2的所有后代

3是孩子。孙子孙女可以是任何父ID为3的帖子。等等

在这种情况下,ID 2的后代是ID的3、4和5

ID 7的后代是ID的8、9、10和6

我是否可以在MySQL中不受祖先深度限制地查询这个问题?或者甚至在php中迭代结果集


非常感谢。您必须规范化您的数据库

我理解你的问题是这样的:

论坛家长 --第一论坛-- 论坛id=1 论坛名称=php

线程子对象

--第一记录-- 螺纹内径=20 论坛id=1 --下一张唱片-- 螺纹id=21 论坛id=1

后孙

然后使用连接访问上下 例如,使用规范化数据库 如果你在线程表中,你可以访问它所属的论坛
除了特定线程中的帖子外,所有内容都在一个查询中

您必须规范化数据库

我理解你的问题是这样的:

论坛家长 --第一论坛-- 论坛id=1 论坛名称=php

线程子对象

--第一记录-- 螺纹内径=20 论坛id=1 --下一张唱片-- 螺纹id=21 论坛id=1

后孙

然后使用连接访问上下 例如,使用规范化数据库 如果你在线程表中,你可以访问它所属的论坛
以及您特定线程中的帖子,都可以在一个查询中完成:我将使用伪代码给您一个大致的想法

$res = get_res('select * from my_table');

$descendants = get_descendants(2); 

function get_descendants ($parent_id, $res) {
    foreach ($res as $row) {
        if ($row['parent_id'] == $parent_id) {
            $descendants[] = $row['id']; 
            $descendants = $descendants + get_descendants($row['id'], $res);
        }
    }
    return $descendants;
}

您可以这样完成:我将使用伪代码给您一个粗略的想法

$res = get_res('select * from my_table');

$descendants = get_descendants(2); 

function get_descendants ($parent_id, $res) {
    foreach ($res as $row) {
        if ($row['parent_id'] == $parent_id) {
            $descendants[] = $row['id']; 
            $descendants = $descendants + get_descendants($row['id'], $res);
        }
    }
    return $descendants;
}
getTree将返回包含嵌套子节点的父节点。您可能需要根据您的框架更改PHP mysql查询

public function getTree($nodeId) {
    $mainNode = $this->db->select('id, content')->from('table_name')->where('id', $nodeId)->get()->first_row();
    $this->recur($mainNode);
    return $mainNode;
}

public function recur(&$node) {
    $q = $this->db->select('id, content')->from('table_name')->where('parent_id', $node->id)->get();
    if($q->num_rows() == 0) {
        return;
    }

    if($q->num_rows() > 0) {
        $node->children = $q->result(); // array of all child nodes
        foreach ($node->children as $u) {
            $this->recur($u);
        }
    }
}
getTree将返回包含嵌套子节点的父节点。您可能需要根据您的框架更改PHP mysql查询

public function getTree($nodeId) {
    $mainNode = $this->db->select('id, content')->from('table_name')->where('id', $nodeId)->get()->first_row();
    $this->recur($mainNode);
    return $mainNode;
}

public function recur(&$node) {
    $q = $this->db->select('id, content')->from('table_name')->where('parent_id', $node->id)->get();
    if($q->num_rows() == 0) {
        return;
    }

    if($q->num_rows() > 0) {
        $node->children = $q->result(); // array of all child nodes
        foreach ($node->children as $u) {
            $this->recur($u);
        }
    }
}

OP的现有结构是标准的,并且得到了很好的支持。您的结构要求每个级别都有一个新表。OP的现有结构是标准的,并且支持良好。您的结构要求每个级别都有一个新表。