我在我们PHP/MySQL驱动的商店中非常努力地使用嵌套集查询,我将以电子部分为例。MySQL嵌套集搜索
类别存储为嵌套集模型(左,右和深度)。
随着客户导航通过实体店,他们可以按品牌,类别,价格范围等筛选的产品清单
比方说,客户是由索尼观看的所有产品。索尼将拥有计算,电视,音频和保护设备产品。
但是索尼产品不存储在这些类别(但可能是),它们被存储在这些主要类别的子类别,如:
Televisions > LCD > Widescreen
Televisions > CRT
Computing > Optical Drives > DVD-RW
Computing > Input Devices > Wireless > Keyboards
Audio > Portable > MP3
Household Appliances
客户选择索尼后,我希望他们有按类别来缩小选项,所以他们最初将有以下选择:
Televisions
Computing
Audio
Household Appliances
但我只能这样做带回具体的类别列表查询:
Widescreen
CRT
DVD-RW
Keyboards
MP3
Household Appliances
我需要的主要类别来展示,那么如果客户选择电视,他们将得到的选择:
LCD
CRT
谁能帮我这个问题,最好返回的信息尽可能快,与最小的递归(因此首先使用嵌套集合)或太多的查询,我将非常感激。
下面是我用它来获取子类别列表中查询,如果它与数据库的结构有助于:
select c.categories_id, cd.categories_name, c.parent_id, c.lft, c.rgt, c.dpth
from categories c
inner join categories_description cd on cd.categories_id = c.categories_id
inner join products_to_categories p2c on p2c.categories_id = c.categories_id
inner join products p on p.products_id = p2c.products_id
where cd.language_id='1'
and c.lft between 3489 and 3670
and c.categories_status = '1'
group by c.categories_id
order by sort_order, cd.categories_name
随着数据库原本是adjacancy模型结构,每个类别ID也有父如果简化了任何内容,也会存储ID。
谢谢。
我都有种设法得到它的工作,但它是非常缓慢!!任何想法索引或改变它的工作方式? – Paul 2011-06-15 14:14:52
选择\t \t cd。*,祖先。* FROM \t \t'fec_categories'孩子 JOIN \t \t'fec_categories'祖先打开之间ancestor.'lft' child.'lft'和ancestor.'rgt' JOIN \t \t'fec_categories_description' CD上ancestor.'categories_id' = cd.'categories_id' JOIN上child.categories_id = p2c.categories_id \t \t fec_products_to_categories P2C JOIN \t \t fec_products上p.products_id = p2c.products_id WHERE \t \t p.'manufacturers_id' = 6 AND \t \t子页。 '美食gories_status' = 1 AND \t \t ancestor.'categories_status' = 1 AND \t \t cd.'language_id' = 1 AND ancestor.'dpth' = 1 GROUP BY ancestor.'categories_id' ORDER BY \t ancestor.' lft' – Paul 2011-06-15 14:15:51
我最终放弃了这一点,转而采用Bill Karwin的传递闭包表法,这更适合我们的要求。 – Paul 2011-06-30 10:25:15