title: MySQL select category and subcategory if they have products
body: <p>I'm trying to make a query which resolves this:</p>
  
  <p>Select main category if it contains product(s) OR it has a subcategory that contains product(s)</p>
  
  <p>the problem is that it doesn't select subcategories with containing products</p>
  
  <pre><code>SELECT Category.id, Category.name, Category.description, Category.image
  FROM categories_table as Category
  JOIN products_table as Product
  WHERE Category.subcategory = 0 AND Product.category = Category.id    
      OR Category.id IN
      (
          SELECT SubProduct.categorie
          FROM kosBonusShop_regels AS SubProduct
          WHERE SubProduct.category = Category.subcategory
              AND Product.category = Category.id   
          GROUP BY Product.category
      )
  GROUP BY Product.category
  
  
  Product #1                               Category #1
  id             | 1                       id          |  1
  category       | 2 (Pencils)             subcategory |  0 (none)
  description    | Pencil                  title       |  office supplies
  
  Category #2
  id          | 2
  subcategory | 1 (office supplies)
  title       | Pencils
  </code></pre>
  
  <p>Product Pencil should be in the Category Pencils and Category Pencils should be an Subcategory of the Category Office Supplies</p>
  
  <p>Also the Category Office Supplies Can contain products</p>