title: If isn't null, use "where"
body: <h2>The goal</h2>
  
  <p>Use the clause "where" only if parameter is not null.</p>
  
  <h2>The problem</h2>
  
  <p>I do not know the syntax.</p>
  
  <h2>What I have</h2>
  
  <p>The follow syntax that doesn't work.</p>
  
  <pre><code>CREATE DEFINER=`root`@`localhost` PROCEDURE `getProductsListForHome`
  (IN `inOffer` INT, IN `categoryId` INT)
  BEGIN
      SELECT (MIN(`map`.`Product_Price`)) as `minProductPrice`,
          (MAX(`map`.`Product_Price`)) as `maxProductPrice`,
          `pr`.`Product_Name` as `productName`,
          `ca`.`Category_Name` as `categoryName`
      FROM `bm_market_products` as `map`
      JOIN `bm_products` as `pr` ON `map`.`Product_Id` = `pr`.`Product_Id`
      JOIN `bm_products_category_relationship` as `car` 
      ON `pr`.`Product_Id` = `car`.`Product_Id`
      JOIN `bm_product_categories` as `ca` ON `car`.`Category_Id` = 
      `ca`.`Category_Id`
  
      WHERE `map`.`Product_State` = inOffer
  
      IF (`categoryId` != null) THEN
          AND `ca`.`Category_Id` = `categoryId`
      END IF;
  
      GROUP BY `map`.`Product_Id`;
  END
  </code></pre>
  
  <p>The problem is at line 19.</p>
  
  <h2>Duplicate question?</h2>
  
  <p>I don't think so. I search about this subject, but without sucess — then I came here to post.</p>
  
  <h2>Details</h2>
  
  <p><a href="http://dev.mysql.com/doc/refman/5.0/en/control-flow-functions.html" rel="nofollow">I read about Control Flow Functions here</a>, but it is still confusing to me.</p>
  
  <p>Thanks in advance!</p>