电子商务网站上的嵌套模型菜单
我有一个带有类别表的数据库,使用 MySql 中的嵌套方法。但我总是碰壁。该网站包含“部分”物种(因为这是一家宠物商店)。我已经有一个查询,该查询返回包含每个物种的产品的所有类别。
现在我的问题是我希望能够只显示包含产品的类别。例如,如果我使用儿童成人
创建类别food
。那么系统不应该在任何物种下列出该类别。但是,一旦我在 adult
类别下创建一个 Product,系统就会列出 Food
以及子 Adult
。
该表目前的结构如下:
Category:
- id
- top
- name
- lft
- rgt
- 创建的(为了完整性)
- lastupdated (为了完整性)
所以我能够从表中为每个物种构建一个完整的树,但我需要类别只有在包含该物种的产品后才可见。
听起来很简单,但由于某种原因,我无法理解这一点。有帮助吗?解决这个问题的最佳方法是什么?
更新:
我用来获取类别的查询:
SELECT node.id as nid, node.top as top, node.*, brands.name as brandname from
brands, foods, foodcategories, categories as node, categories as parent WHERE
node.lft BETWEEN parent.lft AND parent.rgt AND parent.top=1 AND
foodcategories.food=foods.id AND foodcategories.category=node.id
AND brands.id=foods.brand AND foods.species={speciesid}
然后我执行一个查询来获取所有类别及其 lft、rgt。我可以用它来建造树。
I have a Database with a category table using the Nested Approach in MySql. But I keep hitting a wall. The Site contains "sections" species (As this is a pet store). I already have a query that returns all the categories that have products in them for each species.
Now my problem is that I want to be able to only show categories that have products under them. So for example if I create the category food
with a child adult
. Then the system should not list the category under any species. But as soon as I create a Product under the adult
category the system should list Food
with the child Adult
.
The Table is currently structured as follows:
Category:
- id
- top
- name
- lft
- rgt
- created (for the sake of completeness)
- lastupdated (for the sake of completeness)
So I am able to build a full tree from the table for each species but I need the categories to only be visible once they have products in them for the species.
Sounds simple enough but for some reason I can't get my brain around this. Any Help? What would be the best way to approach this?
Update:
The query I'm using to get the Categories:
SELECT node.id as nid, node.top as top, node.*, brands.name as brandname from
brands, foods, foodcategories, categories as node, categories as parent WHERE
node.lft BETWEEN parent.lft AND parent.rgt AND parent.top=1 AND
foodcategories.food=foods.id AND foodcategories.category=node.id
AND brands.id=foods.brand AND foods.species={speciesid}
I then execute a query to get all the categories and their lft,rgt. Which I can built the tree from.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
像这样的东西应该可以工作:
或者
您将需要匹配您的字段并将其余代码写入输出,但这应该可以帮助您开始。您还需要更改 a.* 以列出用于更好优化的所有字段。
编辑:
我理解你想要完成的任务,但仍然不理解你的数据库结构。对于 1 级子类别,我会这样做:
您可以使用它并适应您的代码,或者如果您进一步解释一下您的数据库结构,我可以提供进一步的帮助。
Something like this should work:
OR
You will need to match up your fields and write the rest of the code to output, but this should get you started. You also will want to change a.* to list all your fields being used for better optimization.
EDIT:
I understand what you are trying to accomplish I think, but still don't understand your database structure. For 1 level of subcategories, I would do something like:
You can take this and adapt to your code or if you explain a little more about your database structure, I can help further.
经过几天的努力,终于想到了一个解决方案并实施了。
我可以执行以下查询并获取进出类别的完整路径,
然后我想如果可以的话为什么不在类别查询中包含此路径。因此,当我检索包含产品的类别时,我可以简单地解析该字符串并将 id 与我也检索到的类别列表相匹配。
这有点循环。所以我缓存了菜单并且每 5 分钟只生成一次。
就是这样! 3 条查询!
其中的产品。
具有一定的ID。
迎接下一个挑战!
After a couple of days struggling a finally thought of a solution and implemented it.
I can execute the following query and get the entire path to and from a category,
then I thought if I could this why not include this path in the category query. So when I retrieve the categories that have products in them I can simply parse this string and match the id to a list of categories that I also retrieve.
This is quite a bit of looping. So I cache the menu and only generate it once every 5 min.
So that's it! 3 Queries!
products in them.
with a certain ID.
On to the next challenge !