title: Mysql compare values in table
body: <p>I'm using the following query in order to compare values from two tables. SUM(u.val) is the value of total payments and I want to compare that value with the value of the invoice (r.p_val) and get listed only those with a lower value od SUM.</p>
  
  <pre><code>SELECT u.nmb, SUM(u.val) AS total_val,r.p_val
  FROM inv u
  INNER JOIN bills r ON r.nmb = u.nmb
  WHERE total_val &lt; r.p_val  
  GROUP BY u.nmb
  </code></pre>
  
  <p>With this query I get error "Unknown column 'total_val' in 'where clause'"</p>
  
  <p>I only managed to get a result with a query that compares the individual values and not the total payment. That query looks like this:</p>
  
  <pre><code> SELECT u.nmb, u.val, SUM(u.val) AS total_val,r.p_val
   FROM inv u
   INNER JOIN bills r ON r.nmb = u.nmb
   WHERE  u.val &lt; r.p_val  
   GROUP BY u.nmb
  </code></pre>
  
  <p>Is there a way to do some similar query and get the required result?</p>
  
  <p>Thanks for any suggestions.</p>