web-dev-qa-db-ja.com

SELECTリストはGROUP BY句に含まれておらず、非集計列が含まれています

次のエラーを受け取ります:

Expression #2 of SELECT list is not in GROUP BY clause and contains nonaggregated column 'world.country.Code' which is not functionally dependent on columns in GROUP BY clause; this is incompatible with sql_mode=only_full_group_by

次のクエリを実行する場合:

select countrylanguage.language, country.code, sum(country.population*countrylanguage.percentage/100)
from countrylanguage
join country on countrylanguage.countrycode = country.code
group by countrylanguage.language
order by sum(country.population*countrylanguage.percentage) desc ;

MySQLワールドテストデータベースの使用( http://dev.mysql.com/doc/index-other.html )。なぜこれが起こっているのか分かりません。現在、MYSQL 5.7.10を実行しています。

何か案は??? :O

28

@ブライアン・ライリーがすでに言ったように、選択から1列を削除する必要があります

select countrylanguage.language ,sum(country.population*countrylanguage.percentage/100)
from countrylanguage
join country on countrylanguage.countrycode = country.code
group by countrylanguage.language
order by sum(country.population*countrylanguage.percentage) desc ;

またはグループに追加します

select countrylanguage.language, country.code, sum(country.population*countrylanguage.percentage/100)
from countrylanguage
join country on countrylanguage.countrycode = country.code
group by countrylanguage.language, country.code
order by sum(country.population*countrylanguage.percentage) desc ;
24
davejal

country.codegroup byステートメントに含まれておらず、集約ではありません(集約関数にラップされています)。

https://www.w3schools.com/sql/sql_ref_sqlserver.asp

1
Brian Riley