1. ホーム
  2. mysql

[解決済み] エラーコード1111。グループ関数の無効な使用

2022-02-12 09:39:22

質問内容

これが効くんですね。

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

しかし、私は30以上のcity_population_percentの値のみを取得する必要があるので、これを試してみました。

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'
**AND ROUND(100*(SUM(ci.population)/c.population)) > 30**
GROUP BY c.name

そして、その時、私は

エラーコード 1111。グループ関数の無効な使用

つまり、この条件を WHERE :

AND ROUND(100*(SUM(ci.population)/c.population)) > 30

解決方法は?

そこで、この条件を 持っている

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