title: MySQL having SUM(column1) <> column2 does not work
body: <p>I'm working on a query that uses having to compare a sum of elements from a joined table to a value of a field from the "main" table.
  Here's the gist of my query:</p>
  
  <pre><code>SELECT t1.id
  FROM table1 AS t1
  INNER JOIN table2 t2 ON t2.t1_id = t1.id -- AND OTHER CONDITIONS
  -- WHERE more t1 conditions
  GROUP BY t1.id
  HAVING SUM(t2.amount) &lt;&gt; t1.total_amount
  AND SUM(t2.amount) = (t1.component1+t1.component2+t1.component3+t1.component4);
  </code></pre>
  
  <p>It outputs : ERROR 1054 (42S22): Unknown column 't1.total_amount' in 'having clause'
  Needless to say, the field exists in t1 and there were no typos (quadra-checked this, on the brink of insanity here). </p>
  
  <hr>
  
  <p>I can't for the life of me figure out what's wrong here. I tried searching SO already and most of the having problems are due to people using HAVING instead of WHERE, but seeing as I'm using SUM(), I think this is the right way to go.</p>
  
  <hr>
  
  <p>EDIT: Terje's answer was the most accurate, although I did manage to pull it off without using that syntax in (possibly the most stupid update sentence of all time) my update sentence, which was (stupid, do not try this at home unless everything else fails):</p>
  
  <pre><code>UPDATE t1
  SET t1.total_amount = 
  (
    SELECT SUM(t2.amount) 
    FROM table 2
    --WHERE, AND 
    GROUP BY t1.id
    HAVING SUM(t2.amount) &lt;&gt; t1.total_amount
    AND SUM(t2.amount) = (t1.component1+t1.component2+t1.component3+t1.component4)
  )
  WHERE t1.id IN (same awful subquery as before, except selecting t2.t1_id)
  --AND other conditions
  ;
  </code></pre>
  
  <p>P.S. : The end goal here is to use the same having conditions in an update sentence
  Bunch of thanks in advance,
  -Lauri</p>