0

Let's say I have a bunch of businesses. And each business can have multiple categories, subcategories and sub-subcategories (three levels). Let's say I set up a table according to the nested set model for my categories.

How do I now use this table and assign categories to each business? I understand I will need another table but what node gets assigned? Is it the lowest level node?

business_id category_id

And then what's the right way to retrieve all the categories for each business?

Shaan
  • 863
  • 2
  • 12
  • 25

1 Answers1

0

The way this generally works is that you assign the leaf or lowest-level-node. Then when you are querying to get the full hierarchy you traverse up the tree to the root. It is generally much easier (especially in MySQL) to traverse from leaf to root then vice versa.

Here is the best link I've found that describes how to accomplish this query for a tree of dynamic size (the link you've included assumes that the tree is always 3 levels deep)

Ben English
  • 3,900
  • 2
  • 22
  • 32
  • Thanks but now I'm confused on how to get the tree (with depth) for a specific low-level node. Let me read that article you sent and I'll get back to you. – Shaan Feb 08 '12 at 03:06
  • Pay attention only to the first portion. In the end it gets more complicated. If you have any questions or need explanations I'll gladly help. This sort of thing I actually find interesting. – Ben English Feb 08 '12 at 15:55