关于mysql Expression #1的解决方案

完美解决

重装了mysql之后,前台页面获取后台数据老是失败,出现的错误如下

Cause: com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException:
 Expression #1 of SELECT list is not in GROUP BY clause and contains nonaggregated column 'db_srkj_server.t.id' which is not functionally dependent on columns in GROUP BY clause; this is incompatible with sql_mode=only_full_group_by
; bad SQL grammar []; nested exception is com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException: Expression #1 of SELECT list is not in GROUP BY clause and contains nonaggregated column 'db_srkj_server.t.id' which is not functionally dependent on columns in GROUP BY clause; this is incompatible with sql_mode=only_full_group_by

能看出来是关于only_full_group_by的使用错误。查阅了资料才知道,mysql 5.7后默认开启
sql_mode=only_full_group_by,自己可以去查询一下sql_mode,第一个就是。这个配置的主要意思呢就是select之后的列必须被group by之后的列所包含,简单来说就是:
select a,b from table group by a,b,c; (正确)
select a,b,c from table group by a,b; (错误)
所以一般不使用这个配置。

set @@global.sql_mode 
=’STRICT_TRANS_TABLES,NO_ZERO_IN_DATE,NO_ZERO_DATE,ERROR_FOR_DIVISION_BY_ZERO,NO_AUTO_CREATE_USER,NO_ENGINE_SUBSTITUTION’;

如图设置之后就可以访问后台数据了。还可以把my.ini文件中添加
sql_mode= STRICT_TRANS_TABLES,NO_ZERO_IN_DATE,NO_ZERO_DATE,ERROR_FOR_DIVISION_BY_ZERO,NO_AUTO_CREATE_USER,NO_ENGINE_SUBSTITUTION,就不用每次启动mysql再去修改了。

你可能感兴趣的:(mysql)