在服务器运行正常,到本机运行就出现错误
Expression #1 of SELECT list is not in GROUP BY clause and contains nonaggregated column ‘mgl.db_mglmro_customer_customercar.id’ which is not functionally dependent on columns in GROUP BY clause; this is incompatible with sql_mode=only_full_group_by
查阅了相关资料,得出一下结论:
mysql5.7.x版本,默认是开启了 only_full_group_by 模式的,在开启这个模式后,原先的 group by语句就报错,所以解决的方法就是把这个模式删除掉.
在MySQL数据库中执行
select @@global.sql_mode;
结果如下:可以看到第一行的 only_full_group_by
重新设置sql_midel,执行如下语句
set @@global.sql_mode ='STRICT_TRANS_TABLES,NO_ZERO_IN_DATE,NO_ZERO_DATE,ERROR_FOR_DIVISION_BY_ZERO,NO_AUTO_CREATE_USER,NO_ENGINE_SUBSTITUTION';
然后再次查询就可以了:
一旦开启 only_full_group_by ,感觉,group by 将变成和 distinct 一样,只能获取受到其影响的字段信息,无法和其他未受其影响的字段共存,这样,group by 的功能将变得十分狭窄了
only_full_group_by 模式开启比较好。
因为在 mysql 中有一个函数: any_value(field) 允许,非分组字段的出现(和关闭 only_full_group_by 模式有相同效果)。