[英]Select all parents records in mysql
Table structure:表结构:
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
Suppose If I am on a forth or fifth level children category, How to retrieve all its parent categories in a query.假设如果我在四级或五级子类别中,如何在查询中检索其所有父类别。 Basically I want to show breadcrumbs hierarchy.
基本上我想显示面包屑层次结构。
You can try with:您可以尝试:
select @start := id as 'id', title, parent
from table1
join
(select @start := 0) temp
where parent = @start and id <= 4;
Note that without the id < 4
check, you'll get the full tree from the "top" ( 0
) to the "bottom" ( 6
).请注意,如果没有
id < 4
检查,您将获得从“顶部”( 0
)到“底部”( 6
)的完整树。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.