【问题标题】:SELECT list is not in GROUP BY clause and contains nonaggregated column [duplicate]SELECT 列表不在 GROUP BY 子句中,并且包含非聚合列 [重复]
【发布时间】:2016-04-27 01:55:41
【问题描述】:

收到以下错误:

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

【问题讨论】:

  • 您启用了ONLY_FULL_GROUP_BY 选项,这消除了MySQL 对GROUP BY 的宽松规则。
  • MySQL 5.7 中该选项的默认值已更改。
  • @Barmar 从什么变成什么?
  • @OlleHärstedt 从关闭到打开。

标签: mysql sql aggregate mysql-error-1055


【解决方案1】:

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

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

【讨论】:

  • 链接失效了,请在回答时尝试发布实际内容代替超链接。
  • 这不是关于 SQL Server 的问题。
【解决方案2】:

正如@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 ;

或将其添加到您的分组中

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 ;

【讨论】:

  • 或者直接从mysql命令行运行:SET GLOBAL sql_mode=(SELECT REPLACE(@@sql_mode,'ONLY_FULL_GROUP_BY',''));
  • 运行但没有效果
  • 我必须在会话中另外设置它:SET SESSION sql_mode=(SELECT REPLACE(@@sql_mode,'ONLY_FULL_GROUP_BY',''));
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2016-12-12
  • 2017-07-29
  • 2017-06-22
  • 2016-09-26
  • 1970-01-01
  • 2018-06-12
  • 2019-01-22
相关资源
最近更新 更多