MySQLテーブルのネストされたセットを使用して、カテゴリの階層と、製品を説明する追加のテーブルを記述しています。
カテゴリテーブル;
id
name
left
right
製品表;
id
categoryId
name
製品のすべての親カテゴリを含むフルパスを取得するにはどうすればよいですか?すなわち:
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`