我的类别结构如下

 

我想选择叶节点。我的意思是没有子类别的类别。

在我的数据库监视器中,CPU,小说和漫画将是答案。

任何帮助将不胜感激。

编辑:
我试过了

public function get_valid_categories($parent)
        {
                $has_childs = false;
                foreach($this->categories as $key => $value) {
                    if ($value['parent'] == $parent) {
                        if ($has_childs === false) {
                            $has_childs = true;
                        }
                        else
                        {
                            $this->valid_categories[] =  $value['name'];
                        }
                        $this->get_valid_categories($key);

                    }
                }
                if ($has_childs === true)
                {
                    return $this->valid_categories ;
                }
        }


我按如下方式调用此函数

get_valid_categories(0);

最佳答案

您不需要为此的递归查询。下面的SQL怎么样:

select t1.*
from table t1
    left join table t2 on t1.id = t2.parent_id
where t2.id is null


这将获取表中的行并自我联接以获取每一行的子级。然后,通过检查t2.id is null过滤掉那些没有子行的行。

关于php - 通过递归查询获取叶节点,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/7528672/

10-12 01:28
查看更多