Expression #1 of SELECT list is not in GROUP BY clause and contains nonaggregated column

发布时间 2023-09-25 11:03:10作者: 八英里

MySQL有any_value(field)函数,他主要的作用就是抑制ONLY_FULL_GROUP_BY值被拒绝

官方有介绍,地址:https://dev.mysql.com/doc/refman/5.7/en/miscellaneous-functions.html#function_any-value

我们可以把select语句中查询的属性(除聚合函数所需的参数外),全部放入any_value(field)函数中;

例如:

SELECT a.template_id,any_value(a.detail) detail, any_value(a.id) id
from template_detail a
group by template_id

 

这样sql语句不管是在ONLY_FULL_GROUP_BY模式关闭状态还是在开启模式都可以正常执行,不被mysql拒绝。

从上面的语句中也能看出来,每个显示的字段都要写any_value(...),这样其实很麻烦。