title: Proper usage of HAVING clause
body: <p>I am working with MySQL and my goal is to <code>UPDATE</code> only fields in a table that satisfy specific condition. Here is what I have:</p>
  
  <pre><code>UPDATE tbl_money AS m 
  SET m.amount = m.amount + (SELECT SUM( b.bn_ppd )
                             FROM tbl_emp AS e
                             LEFT JOIN tbl_bonuses AS b 
                                ON b.bn_id = e.bn_id
                             WHERE m.u_id = e.u_id
                             HAVING COUNT(e.u_id) &gt; 0
                             GROUP BY e.u_id LIMIT 1)
  </code></pre>
  
  <p>With the above I would like to add on the current amount the sum of bonuses that an employee has and only to the employees who exist in <code>tbl_emp</code>. As it is, I am getting an error message:</p>
  
  <blockquote>
    <p>1064 - You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'GROUP BY o.user_id LIMIT 1)' at line 7 </p>
  </blockquote>
  
  <p>Without <code>HAVING</code> clause, the system updates amount of all employees but those who don't have any bonus, it just resets the amount to 0 (even if they have an amount already). To those who have bonus, is working fine.</p>
  
  <p>Any ideas? What is the proper way to use <code>HAVING</code>?</p>
  
  <p>UPDATE:</p>
  
  <pre><code>tbl_bonuses
  -----------
  bn_id | bn_ppd
  1       5
  2       2
  3       4
  4       10
  
  tbl_emp
  -------
  emp_id | u_id | bn_id | ...
  1       30      2
  2       30      3
  3       31      1
  
  tbl_money
  ---------
  m_id | u_id | amount
  1      30     100
  2      31     56
  3      32     35
  </code></pre>
  
  <p>After the update the desired result should be</p>
  
  <pre><code>    tbl_money
      ---------
      m_id | u_id | amount
      1      30     106
      2      31     57
      3      32     35
  </code></pre>
  
  <p>However, I am getting this result,</p>
  
  <pre><code>    tbl_money
      ---------
      m_id | u_id | amount
      1      30     106
      2      31     57
      3      32     0
  </code></pre>