mysql报错1140 In aggregated query without GROUP BY, expression #2 of SELECT list contains nonaggregate

mysql报错1140 In aggregated query without GROUP BY, expression #2 of SELECT list contains nonaggregate

数据库语句报In aggregated query without GROUP BY, expression #1 of SELECT list contains nonaggregated column 'szmz_zhbz_gov.b.cremation_time'; this is incompatible with sql_mode=only_full_group_by 

经查阅资料得知,在MySQL5.7版本之后对group by进行了优化。默认启动改进之后的版本启动了ONLY_FULL_GROUP_BY模式。

即ONLY_FULL_GROUP_BY是MySQL数据库提供的一个sql_mode,通过这个sql_mode来保证SQL语句“分组求最值”合法性的检查。这种模式采用了与Oracle、DB2等数据库的处理方式。即不允许select target list中出现语义不明确的列。

解决方法:

mysql报错1140 In aggregated query without GROUP BY, expression #2 of SELECT list contains nonaggregate

只要有聚合函数sum(),count(),max(),avg()等函数就需要用到group by