MySQL SELECT 列表不在 GROUP BY 子句中并且包含非聚合列

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/34913281/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-31 21:32:59  来源:igfitidea点击:

SELECT list is not in GROUP BY clause and contains nonaggregated column

mysqlsqlaggregatemysql-error-1055

提问by Danny J. Williams

Receiving the following error:

收到以下错误:

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

When running the following query:

运行以下查询时:

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 ;

Using the MySQL world test database (http://dev.mysql.com/doc/index-other.html). No idea why this is happening. Currently running MYSQL 5.7.10.

使用 MySQL 世界测试数据库 ( http://dev.mysql.com/doc/index-other.html)。不知道为什么会这样。当前运行 MYSQL 5.7.10。

Any ideas??? :O

有任何想法吗???:O

回答by davejal

As @Brian Riley already said you should either remove 1 column in your select

正如@Brian Riley 已经说过的,您应该在选择中删除 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 ;

or add it to your grouping

或将其添加到您的分组中

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 ;

回答by Brian Riley

country.codeis not in your group bystatement, and is not an aggregate (wrapped in an aggregate function).

country.code不在您的group by语句中,也不是聚合(包装在聚合函数中)。

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

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