title: Trigger syntax and IF ELSE THEN
body: <p>I'd like to create a trigger which count the number of rows with a specific id (id_ort). 
  If it found more than 5 rows, I need to increment a variable.</p>
  
  <h2>Trigger Syntax</h2>
  
  <pre><code>BEGIN
  DECLARE nb INT;
  DECLARE nba INT;
  
  SET nba =0;
  
  
  SET NEW.`VPLS_ID_NodeB` = CONCAT("21100", LPAD(NEW.`VPLS_ID_NodeB`,4,0));
  
  
  SET nb = (SELECT COUNT(DISTINCT(`VPLS_ID_aggregation`)) FROM `VPLS_nodeB` WHERE `id_ORT` = NEW.`id_ORT`);
  
  IF(nb &gt; 5) THEN
  SET nba = nb + 1;
  ELSE
  SET nba = nb;
  END IF;
  
  
  SET NEW.`VPLS_ID_aggregation` = CONCAT("21188", LPAD(NEW.`id_ORT`,2,0), LPAD(nba,2,0));
  END
  </code></pre>
  
  <p>However, there is a bug... Even if i've less than 5 rows, the var is incremented each time.</p>
  
  <p>Any ideas? Maybe it's a syntax problem...</p>
  
  <p>Thanks a lot!</p>