I'm making a tree of products and categories. the tree accepts the products to be exist in any category whether it is a parent node or a leaf.
here are my tables
#category table
+------+-----------+-------------+
| ID | cat_name | parent_cat |
+------+-----------+-------------+
| 1 | TV | NULL |
+------+-----------+-------------+
| 2 | LCD | 1 |
+------+-----------+-------------+
| 3 | PLASMA | 1 |
+------+-----------+-------------+
#product table
+------+-----------+-----------+
| ID | prd_name | prd_cat |
+------+-----------+-----------+
| 1 | LG | 1 |
+------+-----------+-----------+
| 2 | HD | 2 |
+------+-----------+-----------+
| 3 | FHD | 2 |
+------+-----------+-----------+
what I want is to make a select query to retrieve the full tree containing all the categories and products.
I can retrieve the categories with this query
select cat1.cat_name lvl1, cat2.cat_name lvl2
from category cat1 left join category cat2 on cat1.ID = cat2.parent_cat
where cat1.parent_cat is null;
this gives me something like this
+------+------+
| lvl1 | lvl2 |
+------+------+
| TV | LCD |
+------+------+
| TV |PLASMA|
+------+------+
when I tried to join the products in the tree I used this query
select cat1.cat_name,cat2.cat_name,product.prd_name
from category cat1 left join category cat2 on cat1.ID = cat2.parent_cat
left join product on cat1.ID = product.prd_cat
where cat1.parent_cat is null;
but that sql gives me this result
+-----------+-----------+-----------+
| cat_name | cat_name | prd_name |
+-----------+-----------+-----------+
| TV | LCD | LG |
+-----------+-----------+-----------+
| TV | PLASMA | LG |
+-----------+-----------+-----------+
I want the product table to be treated as the category (level 2) table so if a parent node like TV have some child categories and child products, I got them both. How to get a result tree like this one:
+-------------+------------+------------+
| tree_lvl_1 | tree_lvl_2 | tree_lvl_3 |
+-------------+------------+------------+
| TV | LG | NULL |
+-------------+------------+------------+
| TV | LCD | HD |
+-------------+------------+------------+
| TV | LCD | FHD |
+-------------+------------+------------+
| TV | PLASMA | NULL |
+-------------+------------+------------+
TV(category) > LG(product)
TV(category) > LCD(category) > HD(product)
TV(category) > LCD(category) > FHD(product)
TV(category) > PLASMA(category)
edit: this question that you suggested is about building the tree of only categories, I know how to do that. my problem here is I have also "products" table that should join the tree as a node just like the category ! I want category and product tables be treated as one table(node)