问题:使用mysql5.7的错误,因为mysql5.7默认开启了only_full_group_by模式
解决:select 中的字段必须全部在group by中
方法:
select max(users.id), max(users.name), users.age from users group by users.age;
对于GROUP BY聚合操作,如果在SELECT中的列,没有在GROUP BY中出现,那么这个SQL是不合法的,因为列不在GROUP BY从句中
所以对于设置了这个mode的数据库,在使用group by 的时候,就要用MAX(),SUM(),ANY_VALUE()这种聚合函数,才能完成GROUP BY 的聚合操作。
参考:http://blog.csdn.net/v587_lu/article/details/51785371
上面的方法能解决一部分问题,但还有的就不行了,此时可以使用临时表的方法:
select id, name,t.group_id, t.score from (select group_id, min(score) as score from game group by group_id order by min(score)) t inner join game on t.group_id=game.group_id and t.score=game.score
这样就能将表game按group_id进行分组之后找到score最小的用户id及name了
参考:http://blog.csdn.net/Allen_Tsang/article/details/54892046