mysql 5.7高版本中group by问题解决办法

select max(user_id) as user_id,`create_time` from silence where user_id in (1, 2, 3, 4) group by user_id desc;

我使用如上语句进行查找的时候,竟然报错了。

Expression #2 of SELECT list is not in GROUP BY clause and contains nonaggregated column 'silence.create_time' which is not functionally dependent on columns in GROUP BY clause; this is incompatible with sql_mode=only_full_group_by

这是因为5.7版本中做了很多优化,所以导致只能查询group了的key,其余的key不能查询。

如果不想修改配置,其实有一个函数可以查询。查询的方式是使用any_value(field)
example

select max(user_id) as user_id,any_value(create_time) as create_time from silence where user_id in (1, 2, 3, 4) group by user_id desc;

猜你喜欢

转载自blog.csdn.net/funnyPython/article/details/84941171
今日推荐