错误代码1111.无效使用组功能

时间:2014-03-03 08:33:03

标签: mysql mysql-workbench mysql-error-1111

这样可行:

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

2 个答案:

答案 0 :(得分:27)

因此,您必须将此条件移至 HAVING 子句

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

答案 1 :(得分:5)

你在where子句中使用聚合函数,这是你在SQL中无法做到的。

请改用HAVING子句:

WHERE c.continent = 'Europe'
GROUP BY c.name
HAVING ROUND(100*(SUM(ci.population)/c.population)) > 30