title: MySQL Unknown column in having clause
body: <p>I'm trying to get all the wins per team, however, SQL decides to throw an error</p>
  
  <p><img src="https://i.stack.imgur.com/8BoHy.png" alt="enter image description here"></p>
  
  <p>The following query is being executed:</p>
  
  <pre><code>SELECT `t`.`teamcode`, COUNT(*) AS `gewonnen`
  FROM `Team` `t`
  INNER JOIN `Wedstrijd` `w` ON `w`.`teamthuis` = `t`.`teamcode`
  GROUP BY `w`.`teamthuis`
  HAVING `w`.`scorethuis` &gt; `w`.`scoreuit`
  
  #1054 - Unknown column 'w.scorethuis' in 'having clause'
  </code></pre>
  
  <p>Without aliases:</p>
  
  <pre><code>SELECT `Team`.`teamcode`, COUNT(*) AS `gewonnen`
  FROM `Team`
  INNER JOIN `Wedstrijd` ON `Wedstrijd`.`teamthuis` = `Team`.`teamcode`
  GROUP BY `Wedstrijd`.`teamthuis`
  HAVING `Wedstrijd`.`scorethuis` &gt; `Wedstrijd`.`scoreuit`
  
  #1054 - Unknown column 'Wedstrijd.scorethuis' in 'having clause'
  </code></pre>