Error Code 1111. Invalid use of group function

前端 未结 2 1758
礼貌的吻别
礼貌的吻别 2021-02-19 00:25

So this works:

SELECT c.name AS country_name, c.population AS country_population, SUM(ci.population) AS city_population, ROUND(100*(SUM(ci.population)/c.populati         


        
相关标签:
2条回答
  • 2021-02-19 00:57

    You're using aggregate functions in a where clause, something you cannot do in SQL.

    Use the HAVING clause instead:

    WHERE c.continent = 'Europe'
    GROUP BY c.name
    HAVING ROUND(100*(SUM(ci.population)/c.population)) > 30
    
    0 讨论(0)
  • 2021-02-19 01:15

    So you have to move this condition to the HAVING clause

    SELECT c.name AS country_name, c.population AS country_population, SUM(ci.population) AS city_population, ROUND(100*(SUM(ci.population)/c.population)) AS city_population_percent
                FROM country AS c
                JOIN city AS ci
                ON c.code = ci.countrycode
    WHERE c.continent = 'Europe'
    GROUP BY c.name
    HAVING ROUND(100*(SUM(ci.population)/c.population)) > 30
    
    0 讨论(0)
提交回复
热议问题