gpt4 book ai didi

mysql - 如何在mysql中显示Parent_id的parent_id直到parent_id为0

转载 作者:行者123 更新时间:2023-11-30 00:37:31 24 4
gpt4 key购买 nike

当我想在类别表上查询数据库时遇到问题,当我想显示 parent_idparent_id 直到 parent_id 是 0。

这是我的尝试 http://www.sqlfiddle.com/#!2/5dc009/1

这就是结果

enter image description here

我的目标是

group_id   |  parent_id   | cat_id | name | variant 

4 | 7 | 4 | Shampo With Conditioner | 1
7 | 1 | 7 | Shampo Woman | null
1 | 0 | 1 | Hair Care | null

最佳答案

Go 递归:

function getRootId($id){
// Select the parent_item which belongs to $id
$query = "SELECT parent_id FROM table WHERE id=".$id." LIMIT 1";
$result = mysqli_query($conn, $query);
$fetch = $result->fetch_assoc();

// If a parent found
if($fetch['parent_id']!==0){
$id = getRootId($fetch['parent_id']); // go up one level
}
return $id;
}

关于mysql - 如何在mysql中显示Parent_id的parent_id直到parent_id为0,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22035991/

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