i have the following DB-Structure / hierarchy:
TABLE product_type:
id,name, ....
TABLE product:
id,parent_id, name, ...
parent_id : is the product_type id
TABLE treeNode:
id, parent_id, name, type
its a tree hierarchy (roots have n sub Nodes) the number of levels of hierarchy is unknown.
the col type
has the values "CATEGORY" OR "GROUP",
that means, i have 2 trees:
Categories:
TreeNode 1
sub 1.1
sub.1.1.1
sub.1.1.2
....
sub 1.2
sub.1.2.1
sub.1.2.2
....
TreeNode 2
sub 2.1
sub.2.1.1
sub.2.1.2
....
sub 2.2
sub.2.2.1
sub.2.2.2
....
Groups:
TreeNode 1
sub 1.1
sub.1.1.1
sub.1.1.2
....
sub 1.2
sub.1.2.1
sub.1.2.2
....
TreeNode 2
sub 2.1
sub.2.1.1
sub.2.1.2
....
sub 2.2
sub.2.2.1
sub.2.2.2
....
TABLE linked_treeNode:
product_id, treeNode_id
lets say now, a user selects:
1: a product type (param: $selected_type
)
2: a category (param: $selected_cat
)
3: a group (param: $selected_group
)
now i would like to display all Products which meets these selections:
1-) linked to selected Catagory or to its Subcategories
AND
2-) linked to selected Group or to its subGroups
AND
3-) linked to selected product type
what is the MySQL statement ( 1 Statement) for that?
i tried this:
SELECT P.* FROM
product P, treeNode C, treeNode G, linked_TreeNode LC
WHERE
p.parent_id='$selected_type'
AND
(
C.type='CATEGORY'
AND
C.parent_id='$selected_cat'
AND
P.id=LC.product_id
AND
(LC.treeNode_id=C.id OR LC.treeNode_id='$selected_cat')
)
AND
(
G.type='GROUP'
AND
G.parent_id='$selected_group'
AND
P.id=LC.product_id
AND
(LC.treeNode_id=G.id OR LC.treeNode_id='$selected_group')
)
;
but i get allways 0 result!
i tried many other Statements(changes), with JOINS ..etc. but without success.
many thanks
EDIT: the Statement i used above is WRONG, so don't use it!