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

标签 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

运行以下查询时:

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

最佳答案

正如@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 - SELECT 列表不在 GROUP BY 子句中并且包含非聚合列,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34913281/

相关文章:

mysql - 对所有类型的字段进行全文搜索是一个好习惯吗?

java - Oracle ojdbc 驱动程序返回 NUMBER 类型列中整数值的浮点结果

MySQL 日期问题 - 如何计算?

python - 如何按级别的计数聚合数据,将每个级别的计数设置为其自己的列?

php - MySQL 同时连接

php - 将 UTF8 名称插入表会出现重复错误

database - Elasticsearch Date直方图存储桶从错误的日期开始

C# LINQ - 如何动态构建 Group By 子句

Python Mysqldb 为位值返回 '\x01'

MySQL,反转 SELECT GROUP BY