title: SQL Query to select products from all categories of a parent
body: <p>I have a categories table like this</p>
  
  <p>Table - categories</p>
  
  <pre><code>cat_id     cat_name         parent_id
  ---------------------------------------
  21         Properties            0
  --------------------------------------
  32         For Rent             21
  ---------------------------------------
  34        Appartments           32
  ---------------------------------------
  35        Houses and villas     32
  --------------------------------------
  36        Builder Floors        32
  -----------------------------------------
  </code></pre>
  
  <p>Product table like this </p>
  
  <p>Table - products</p>
  
  <pre><code>ProductID            ProductName         CategoryID
  ------------------------------------------------------
  39                     villa                 35
  -----------------------------------------------------
  40                     Flat                  35
  ----------------------------------------------------
  41                     appartment            34
  ------------------------------------------------------
  42                     Builder               36
  -----------------------------------------------------
  </code></pre>
  
  <p>I want to fetch all the products exists in the parrent category Properties(21).. also this same query needs to work in case of single category like, in case of <code>cateogryID = 35</code>  i need the products exists in the <code>CategoryID 35</code> only..</p>
  
  <p>Currently iam using the query is:</p>
  
  <pre><code>select p.ProductID, p.ProductTitle,p.SalePrice,p.C_Date,p.ProductShortDescription, c.cat_name, 
  c.cat_id
  from products p JOIN
  (select * from categories where cat_id =32 OR parent_id = 32 ) c
  on p.CategoryID = c.cat_id
  </code></pre>
  
  <p>This query works fine for me... but in case of parrent category Properties(21) its not working.. On all other cases its works fine.</p>
  
  <p>Please help me to alter the above query.. for Properties(21)</p>