I've built a few product databases but I'm never really satisfied with the effiency of the database. My best approach yet is to have one products table, one category table and then one relationship table ie:
Products table:
id product_name data
1 Some prod More info...
2 Another prod Even more...
Category table:
id parent_id cat_name
1 0 Main cat 1
2 1 Sub cat 1
3 1 Sub cat 2
4 3 Sub sub cat 1
5 0 Main cat 2
Relationship table:
id prod_id cat_id
1 1 2
2 1 4
3 2 5
etcetera...
This makes it fast and easy to retrieve the products and also easy to have one product assigned to more than one category.
However the structure for creating the category listing is not as simple as I'd like. First I need to loop the main category, then all the sublevels accordingly. I only like to show categories that has products assigned, but then of course I also need to show all the parent categories if a sublevel contains products. This results in plenty of queries, joins and conditional routines to present the current categories. I wonder if there might be a more efficient structural approach to this problem? You don't need to write my code I just wonder what kind of better principle there might be?