MySQL 嵌套集搜索

发布于 2024-11-15 04:16:19 字数 1461 浏览 2 评论 0原文

我真的很难在我们的 PHP/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

由于数据库最初是邻接模型结构,因此每个类别 ID 还存储了父 ID(如果这可以简化任何事情)。

谢谢。

I'm really struggling with a nested set query in our PHP/MySQL driven shop, I'll use electronics parts as an example.

Categories are stored as a nested set model (left, right and depth).

As the customer navigates through the store, they can filter the list of products by brand, category, price range, etc.

Lets say a customer is viewing all products by Sony. Sony would have products in Computing, Televisions, Audio and Hosuehold Appliances.

But the Sony products aren't stored in those categories (but they could be), they are stored in subcategories of those main categories, such as:

Televisions > LCD > Widescreen
Televisions > CRT
Computing > Optical Drives > DVD-RW
Computing > Input Devices > Wireless > Keyboards
Audio > Portable > MP3
Household Appliances

After the customer has selected Sony, I want them to have the option to narrow down by category, so they would initially have these choices:

Televisions
Computing
Audio
Household Appliances

But I can only do a query that brings back a list of the specific categories:

Widescreen
CRT
DVD-RW
Keyboards
MP3
Household Appliances

I need the main categories to show, then if the customer selects Televisions, they would get the choices:

LCD
CRT

Can anyone help me with this, preferably returning the information as quick as possible, with minimal recursion (hence the use of nested sets in the first place) or too many queries, I would be very grateful.

Here is the query I use to get a list of subcategories if it helps with the structure of the database:

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

As the database was originally an adjacancy model structure, each category id also has the parent ID stored as well if that simplifies anything.

Thanks.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

£冰雨忧蓝° 2024-11-22 04:16:19

根据您的评论,您尝试了其他方法,但我想我还是会回答。

正如您发现的,嵌套集模型在快速高效地检索节点和叶子方面非常出色。然而,一个问题是您发现插入后很难对列表进行排序。

我过去已经克服了这个问题,方法是a)在客户端排序(困难)然后缓存,或者b)确保插入节点/叶子,以便它自动排序(或者甚至使用上/下链接,以便管理员可以在插入发生后自行订购。

我个人喜欢选项 2,并且从未发现它是一个问题。

From your comment you have tried something else but I thought I would answer anyway.

As you found out the nested set model is superb at retrieving nodes and leafs fast and efficiently. However one gotchya is that you found that it is difficult to order the list after the insertion.

I have overcome this in the past by either a) ordering on the client side (difficult) and then caching or b) making sure I insert the node/leaf so it is automatically ordered (or even use up/down links so the admin can order themselves AFTER the insert has happened.

I personally like option 2 and have never found it to be a problem.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文