Вопрос

If I have a nested set for categories, like so:

Widgets
  Blue
  Red
  Green

and I use a relational table to reference products to these categories, e.g.

Products:

id    name
1     Glowing Widget
2     Flying Widget
3     Exploding Widet

Relational table:

id    productId    categoryId
1     1            2
2     2            3
3     3            4

Is there any way you can create a query that will be able to tell how many products are referenced to a category and its children?

Это было полезно?

Решение

SELECT COUNT(DISTINCT productId)
FROM categories
JOIN products_to_categories USING (categoryId)
JOIN products USING (productId)
WHERE categories.left >= {left value of category in question}
AND categories.right <= {right value of category in question}
Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top