我在 MySQL 表中使用嵌套集来描述类别层次结构,并使用附加表来描述产品。
类别表;
id
name
left
right
产品表;
id
categoryId
name
如何检索包含所有父类别的产品的完整路径?IE:
RootCategory > SubCategory 1 > SubCategory 2 > ... > SubCategory n > Product
例如,假设我想列出所有产品SubCategory1
及其子类别,并且每个给定Product
我都想要该产品的完整树路径 - 这可能吗?
这是据我所知 - 但结构不太正确......
select
parent.`name` as name,
parent.`id` as id,
group_concat(parent.`name` separator '/') as path
from
categories as node,
categories as parent,
(select
inode.`id` as id,
inode.`name` as name
from
categories as inode,
categories as iparent
where
inode.`lft` between iparent.`lft` and iparent.`rgt`
and
iparent.`id`=4 /* The category from which to list products */
order by
inode.`lft`) as sub
where
node.`lft` between parent.`lft` and parent.`rgt`
and
node.`id`=sub.`id`
group by
sub.`id`
order by
node.`lft`