gpt4 book ai didi

Mysql邻接: get all parents of the leaf

转载 作者:行者123 更新时间:2023-11-29 01:04:20 25 4
gpt4 key购买 nike

我在使用此数据结构模型的类别中有一个表。还使用另一个表向我显示类别分支的路径:

**t_category**
id | name | parent
------------------
1 mascot null
2 cat 1
3 dog 1
4 doberman 3

**t_category_path**
id | path
------------------
1 /1/
2 /1/2/
3 /1/3/
4 /1/3/4/

我想要的是获取任意项的父项列表。例如,如果我搜索“dog”得到“dog, mascot”,如果我搜索“doberman”应该得到“doberman, dog, mascot”

我试过这个...但是反过来,我的意思是找 parent 拿叶子:

SELECT
c2.id,
c2.name,
p.path
FROM t_category c, t_category c2, t_category_path p, t_category_path p2
WHERE c.id = 1
AND p2.id = c.id
AND p.path LIKE(CONCAT(p2.path,'%'))
AND c2.id = p.id
ORDER BY p.path ASC;

显然得到:

id | name | path
------------------
1 mascot /1/
2 cat /1/2/
3 dog /1/3/
4 doberman /1/3/4/

但我想从“doberman”获得:

id | name | path
------------------
1 mascot /1/
3 dog /1/3/
4 doberman /1/3/4/

是否可以从叶中咨询?

最佳答案

您可能应该将路径字段中描绘的数据分开。使用这样的东西:

id | depth | parent
4 2 3
4 1 1
3 1 1
3 2 3

等...

然后你可以这样做:

SELECT * FROM t_category_path WHERE id = 4

关于Mysql邻接: get all parents of the leaf,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11886756/

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