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

Dan*_*ams 32 mysql sql aggregate mysql-error-1055

收到以下错误:

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
Run Code Online (Sandbox Code Playgroud)

运行以下查询时:

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 ;
Run Code Online (Sandbox Code Playgroud)

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

有任何想法吗???:o

dav*_*jal 25

正如@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 ;
Run Code Online (Sandbox Code Playgroud)

或将其添加到您的分组

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 ;
Run Code Online (Sandbox Code Playgroud)

  • 或者只是从mysql命令行运行:SET GLOBAL sql_mode =(SELECT REPLACE(@@ sql_mode,'ONLY_FULL_GROUP_BY','')); (20认同)
  • 我必须在会话上另外设置它:`SET SESSION sql_mode=(SELECT REPLACE(@@sql_mode,'ONLY_FULL_GROUP_BY',''));` (2认同)

Bri*_*ley -3

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

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

  • 该链接不再有效,请尝试在回答时发布实际内容来代替超链接。 (5认同)