gpt4 book ai didi

php - 根据层次结构显示数据 [祖父 -> 父 -> 子]

转载 作者:可可西里 更新时间:2023-11-01 07:08:40 26 4
gpt4 key购买 nike

您好,我想以如下图所示的层次结构方式显示数据

enter image description here

这是我的数据库表结构

enter image description here

这是我使用过的查询,但它不是我想要的完美结果

SELECT 

t1.parent_id AS primary_Id,
t2.parent_id AS secondary_Id,
t3.parent_id AS teritiary_Id

FROM ucode AS t1
LEFT JOIN ucode AS t2 ON t2.parent_id = t1.id
LEFT JOIN ucode AS t3 ON t3.parent_id = t2.parent_id

输出是

enter image description here

这不是我需要的

还有一种我尝试过的方法

SELECT 

t1.parent_id AS primary_Id,
t2.parent_id AS secondary_Id,
t3.parent_id AS teritiary_Id

FROM ucode AS t1
LEFT JOIN ucode AS t2 ON t2.parent_id = t1.id
LEFT JOIN ucode AS t3 ON t3.parent_id = t2.id

输出是

enter image description here

你能给我一个正确的解决方案吗..

最佳答案

这里是查询

Select gparent, parent, id from ucode as c_tbl,
(SELECT gparent, id as parent from ucode as p_tbl,
(SELECT id as gparent FROM `ucode` WHERE parent_id = 0) as gp_tbl
where p_tbl.parent_id = gp_tbl.gparent) parent_tbl
where c_tbl.parent_id = parent_tbl.parent;

点击Demo结果

关于php - 根据层次结构显示数据 [祖父 -> 父 -> 子],我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49551923/

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