gpt4 book ai didi

php - 如何在 PHP 中使用数组从数据库生成基于 n 级别的父子图形树?

转载 作者:行者123 更新时间:2023-11-30 21:22:00 26 4
gpt4 key购买 nike

我有以下类型的数据要表:

id parent_id child_id 级别
1 53987 52548 1
2 60764 52548 2
3 60764 53987 1
4 60764 59695 2
5 63457 59695 1
6 60764 63457 1

那么,我如何通过级别递归获取数据并将数据存储到数组,如 ['child_id','parent_id',level]。我需要编写查询和生成树的帮助。

树应该是这样的: enter image description here

注意:我无法更改数据库的表结构。我必须需要基于给定表结构的树。

最佳答案

试试这个查询:

select  id,        `child_id`,        `parent_id` , levelfrom    (select * from vtiger_ib_level         order by parent_id, id) products_sorted,        (select @pv := '60764') initialisationwhere   find_in_set(`parent_id`, @pv)and     length(@pv := concat(@pv, ',', id))

关于php - 如何在 PHP 中使用数组从数据库生成基于 n 级别的父子图形树?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46889034/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com