thinkphp5+mysql5.7.20 group方法异常解决方法

thinkphp5+mysql5.7.20 group方法异常解决方法SQLSTATE[42000]: Syntax error or access violation: 1055 Expression #1 of SELECT list is not in GROUP BY clause and contains nonaggregated column #39;movie.t

thinkphp5+mysql5.7.20 group方法异常解决方法

SQLSTATE[42000]: Syntax error or access violation: 1055 Expression #1 of SELECT list is not in GROUP BY clause and contains nonaggregated column 'movie.tp_video_tags.id' which is not functionally dependent on columns in GROUP BY clause; this is incompatible with sql_mode=only_full_group_by



本人开发环境用的是Mysql5.7


出现这个错误的原因是MySQL5.7版本开启了sql_mode中的“only_full_group_by”,而这个在执行以往版本中带有group by的语句时就会报错。

两种解决方法:

1、修改my.cnf(windows下是my.ini)配置文件,删掉only_full_group_by这一项


2、在不修改MySQL配置文件的情况下,需要修改sql语句来执行。

group by后面的列名,还是和以前一样通过select直接获取,而对于select中获取非group by的信息,则要通过any_value()函数。

$tags_list = db('video_tags')
        ->field('any_value(id) as id,any_value(tags_name) as tags_name, any_value(sort) as sort, any_value(stop) as stop')
        ->group('cate_id')
        ->order('sort', 'asc')
        ->select();
        halt($tags_list);


本文标题为:thinkphp5+mysql5.7.20 group方法异常解决方法

基础教程推荐