1. 程式人生 > >mysql 5.7高版本中group by問題解決辦法

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;