sql_mode=only_full_group_by"导致group by失效
我所遇到的场景
辛辛苦苦写好的sql语句,在测试上ok。开开心心上生产,结果报错Error executing query Playground - 1573: (1055,
“Expression #3 of SELECT list is not in GROUP BY clause and contains nonaggregated column ‘newdispatch.r.id’
which is not functionally dependent on columns in GROUP BY clause; this is incompatible with sql_mode=only_full_group_by”)
查原因
MySQL5.7之后,sql_mode中ONLY_FULL_GROUP_BY模式默认设置为打开状态。
ONLY_FULL_GROUP_BY的语义就是确定select target list中的所有列的值都是明确语义,简单的说来,在此模式下,target list中的值要么是来自于聚合函数(sum、avg、max等)的结果,要么是来自于group by list中的表达式的值。
only_full_group_by 模式开启后,原先的 group by 语句就报错,然后又把它移除了。
一旦开启 only_full_group_by ,感觉,group by 将变成和 distinct 一样,只能获取受到其影响的字段信息,无法和其他未受其影响的字段共存,这样,group by 的功能将变得十分狭窄了
only_full_group_by 模式开启比较好。因为在 mysql 中有一个函数: any_value(field) 允许