Question

I have the following nested set.

The cat.table:

cat.ID Name Lft Rgt
  1     A    1   8
  2     B    2   5
  3     C    3   4
  4     D    6   7
  5     E    9   14
  6     F   10   11
  7     G   12   13

OUTPUT: In brackets the count of the items. (In 'A' will count that items, which are in its node, too - B, C and D)

 A (7)
 -B (3)
 --C (2)
 -D (1)
 E (3)
 -F (1)
 -G (2)

The Items table:

ID Cat.ID  Name
1    1     item1
2    2     item2
3    2     item3
4    2     item4
5    3     item5
6    3     item6
7    4     item7
8    6     item8
9    7     item9
10   7     item10

If I select the Cat A than I would like to select that items which are in the B, C and D, too. If I select the B, than show the item from B and C. If I select the F than only show from F... In the Items table I don't have left and right values only tha Cat.ID.

How do I know that if I select the "A" category, which items are in "B", "C" and "D" too?

EDIT: E.g: If I select B than show the items from B and C (the B is parent of C and C is node of B) - output:

item2
item3
item4
item5
item6
Was it helpful?

Solution

The answer is in the Retrieving a full tree paragraph of the linked article.

SELECT Id,Name FROM Items WHERE cat_id IN (
SELECT cat_id
FROM category AS node,
    category AS parent
WHERE node.lft BETWEEN parent.lft AND parent.rgt
    AND parent.name = 'A')
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top