1
votes

i have two tables

Table1 : Categories , 
Columns : id , parent_id , name 
Table2 : products ,   
Columns : id , product_name , category_id , subcategory_id , sale_wanted

Here is the detail
A category can have multiple subcategories id. Parent id 0 means it s a category and parent id != 0 means it is a subcategory. Now each product is related to a subcategory. I need to display the names of category and total subcategories count related to each category. sale 0 means the product is for sale and 1 means it is required. Now i need this.
1.Display all categories and count of subcategories related to each category where the products related to category are for sale.

2

2 Answers

0
votes

The problem it is that your structure is recursive. I think you can search for more informations about intervallic representation.

0
votes

Well i have found this solution

select 
    dc.category_id,
    dc.name ,
    count(ldc.name) as total
from default_category as dc
inner join default_category as ldc on ldc.parent_id= dc.category_id 
inner join(select * from default_products where sale_wanted = 1) as dp on dp.subcategory_id = ldc.category_id
where dc.parent_id = 0
group by dc.category_id 

This works well.