Mazzy Mazzy - 6 months ago 76
SQL Question

MySQL Unknown column in having clause

I'm trying to get all the wins per team, somehow, sql decides to throw an error :S

anybody got a clue?

enter image description here

Here is the query if it's hard to read:

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` > `w`.`scoreuit`

#1054 - Unknown column 'w.scorethuis' in 'having clause'


Without aliases:

SELECT `Team`.`teamcode`, COUNT(*) AS `gewonnen`
FROM `Team`
INNER JOIN `Wedstrijd` ON `Wedstrijd`.`teamthuis` = `Team`.`teamcode`
GROUP BY `Wedstrijd`.`teamthuis`
HAVING `Wedstrijd`.`scorethuis` > `Wedstrijd`.`scoreuit`

#1054 - Unknown column 'Wedstrijd.scorethuis' in 'having clause'

Answer

There is no need to use HAVING. Try WHERE instead:

SELECT `t`.`teamcode`, COUNT(*) AS `gewonnen`
FROM `Team` `t`
INNER JOIN `Wedstrijd` `w` ON `w`.`teamthuis` = `t`.`teamcode`
WHERE `w`.`scorethuis` > `w`.`scoreuit`
GROUP BY `w`.`teamthuis`
Comments