I have a working query that displays a list of categories, sub categories. I also have a working query that only lists main categories but leaves out "this company =1" and the products table. Incorporating this line is what I am have hard time with.
This first one works, and there are no sub categories to display.
This one displays categories and sub categories, but also ones with no products associated with them for that company. I don't want any categories to be listed if there are no products for that company. The condition for the company being "1" is what determines the list of categories. So each of the companies don't have the same list.
SELECT cat.cat_name, cat.cat_id FROM categories as cat
LEFT JOIN category_assoc as assoc
ON assoc.cat_id = cat.cat_id
LEFT JOIN products as pr
ON pr.prod_id = assoc.prod_id
WHERE `". $this->company . "` = '1'
ORDER BY cat.cat_name ASC";
I think I need a inner join? I have categories, category_assoc and products tables
main_categories.cat_id AS main_cat_id
, main_categories.cat_name AS main_cat_name
, child_categories.cat_id AS child_cat_id
, child_categories.cat_name AS child_cat_name
categories AS main_categories
LEFT JOIN categories AS child_categories
ON child_categories.cat_parent = main_categories.cat_id
WHERE main_categories.cat_parent IS NULL