Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Select products where the category belongs to any category in the hierarchy

I have a products table that contains a FK for a category, the Categories table is created in a way that each category can have a parent category, example:

Computers
    Processors
        Intel
            Pentium
            Core 2 Duo
        AMD
            Athlon

I need to make a select query that if the selected category is Processors, it will return products that is in Intel, Pentium, Core 2 Duo, Amd, etc...

I thought about creating some sort of "cache" that will store all the categories in the hierarchy for every category in the db and include the "IN" in the where clause. Is this the best solution?

like image 784
Bruno Avatar asked Oct 13 '08 12:10

Bruno


People also ask

How do I create a new product category?

In the list, find and select the desired record. On the Action Pane, select Product. Select Product categories. Select New. In the Category hierarchy field, enter or select a value. In the Category field, enter or select a value. Select New. In the Category hierarchy field, enter or select a value. In the Category field, enter or select a value.

How do I use category hierarchies to classify products?

Use category hierarchies to classify products for export, procurement, sales, or other purposes. This task is typically performed by a product manager. The example uses product number M0009 in demo data company USMF. Go to Product information management > Products > Released products. In the list, find and select the desired record.

How to add products to the procurement category?

Toggle the expansion of the Productssection. Select Addto associate products with the procurement category. Select the products you want to add to the procurement category. Select the arrow to add the products to the Selectedtable. Select OK. Feedback Submit and view feedback for

What is the best way to select all products in categories?

If you have a relatively small number of total categories, query them all (just ids, parents), collect the ids of the ones you care about, and do a SELECT....IN for the products. This was the appropriate option for me.


1 Answers

The best solution for this is at the database design stage. Your categories table needs to be a Nested Set. The article Managing Hierarchical Data in MySQL is not that MySQL specific (despite the title), and gives a great overview of the different methods of storing a hierarchy in a database table.

Executive Summary:

Nested Sets

  • Selects are easy for any depth
  • Inserts and deletes are hard

Standard parent_id based hierarchy

  • Selects are based on inner joins (so get hairy fast)
  • Inserts and deletes are easy

So based on your example, if your hierarchy table was a nested set your query would look something like this:

SELECT * FROM products 
   INNER JOIN categories ON categories.id = products.category_id 
WHERE categories.lft > 2 and categories.rgt < 11

the 2 and 11 are the left and right respectively of the Processors record.

like image 180
MDCore Avatar answered Nov 15 '22 21:11

MDCore