表结构:
id(int) title(varchar) parent(int)
1 Accessories 0
2 Man 1
3 Women 1
4 Watches 2
5 New Watches 4
6 Used Watches 5
假设如果我位于第四或第五级子类别,如何在查询中检索其所有父类别。基本上我想显示面包屑层次结构。
最佳答案
您可以尝试:
select @start := id as 'id', title, parent
from table1
join
(select @start := 0) temp
where parent = @start and id <= 4;
请注意,如果没有 id < 4
检查,您将获得从“顶部”( 0
)到“底部”( 6
)的完整树。
查看 SQLFiddle ,也是。
关于mysql - 选择mysql中所有 parent 记录,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31179196/