title: PHP / MySQL use SUM result to join table
body: <p>seems like I can´t find the right solution. I have this working query:</p>
  <pre><code>$stateKdnr = $pdo-&gt;prepare(&quot;SELECT *, COUNT(csv_kd_nr) AS cnt, SUM(csv_betrag) as temphf FROM tempimport WHERE csv_zeilenstatus != 'done' GROUP BY csv_kd_nr, csv_name, csv_ort, csv_vorname&quot;);
  </code></pre>
  <p>now I want to join another table with the result temphf. Basically where temphf between von_hf AND bis_hf.  I tried different approaches, fe:</p>
  <pre><code> $stateKdnr = $pdo-&gt;prepare(&quot;SELECT tempimport.*, COUNT(tempimport.csv_kd_nr) AS cnt, SUM(tempimport.csv_betrag) as temphf, hf.* 
              FROM tempimport INNER JOIN hf ON von_hf &gt; temphf AND bis_hf &lt; temphf
              WHERE tempimport.csv_zeilenstatus != 'done' GROUP BY tempimport.csv_kd_nr, tempimport.csv_name, tempimport.csv_ort, tempimport.csv_vorname&quot;);
  </code></pre>
  <p>but here I get the error, unknown column temphf, which makes sense b/c it´s no column but a result.</p>
  <p>Can anyone please help me out?</p>
  <p>Thanks very much!</p>