【问题解决】【sql_mode不兼容】which is not functionally dependent on columns in GROUP BY clause

版权声明:本文为BUG先生原创文章,可任意转载,愿请附上本文链接,谢谢。 https://blog.csdn.net/Edogawa_Konan/article/details/84790695
String sql="select *,count(groupName) as isNum,'Y' as isMark from t_pm_projecttemplate group by groupName order by groupName ";

上述SQL语句报错如下:

Caused by: com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException: Expression #1 of SELECT list is not in GROUP BY clause and contains nonaggregated column 'zjrxmis.t_pm_projecttemplate.id' which is not functionally dependent on columns in GROUP BY clause; this is incompatible with sql_mode=only_full_group_by


原因是什么呢?简单的讲就是select查询的列不包含在group by的列中;

深入分析原因:

已知我的mysql版本如下,

only_full_group_by 使用这个就是使用和oracle一样的group 规则, select的列都要在group中,或者本身是聚合列(SUM,AVG,MAX,MIN) 才行,其实这个配置目前个人感觉和distinct差不多的,所以去掉就好 

 所以修改mysql配置文件如下即可:

sql_mode=STRICT_TRANS_TABLES,NO_ZERO_IN_DATE,NO_ZERO_DATE,ERROR_FOR_DIVISION_BY_ZERO,NO_AUTO_CREATE_USER,NO_ENGINE_SUBSTITUTION

猜你喜欢

转载自blog.csdn.net/Edogawa_Konan/article/details/84790695