MySQL使用any_value()函数解决only_full_group_by报错
any_value 函数是从当前分组中随机返回一个值,可结合group by使用,也可结合窗口函数partition by分区使用,正如以下sql:
selectcreate_time
fromtb_user2
group bymonth(create_time)
;
可能有很多create_time月份相同,mysql不知道显示哪一个,因此会报错:
SELECT list is not in GROUP BY clause and contains nonaggregated column 'test.
tb_user2.create_time'
which is not functionally dependent on columns in GROUP BY clause;
this is incompatible with sql_mode=only_full_group_by
意思是分组后select查询的列必须在group by列表中,
于是就可以用any_value
selectany_value(create_time)
fromtb_user2
group bymonth(create_time)
;
但是any_value返回的数据是组内随机的,确保数据安全准确后使用