mybatis 中 mysql sql_mode=only_full_group_by的错误。

我在项目使用mybits时写过一条sql:

@Select("SELECT name, SUM(feed_Weight) feedWeight,weight FROM aquaculture where name <= #{name} and id_patch = #{idPatch}  group by name ")
    List<FeedWeight> feedWeight(@Param("name") int name, @Param("idPatch") String idPatch) ;

在一台机器测试时没错,放到服务器出错了:

### SQL: SELECT name, SUM(feed_Weight) feedWeight,weight FROM aquaculture where name <= ? and id_patch = ?  group by name
### Cause: com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException: Expression #3 of SELECT list is not in GROUP BY clause and contains nonaggregated column 'waterfowl.aquaculture.weight' 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 #3 of SELECT list is not in GROUP BY clause and contains nonaggregated column 'waterfowl.aquaculture.weight' which is not functionally dependent on columns in GROUP BY clause; this is incompatible with sql_mode=only_full_group_by

这个事因为一些mysql5.7的版本设置了 mysql sql_mode=only_full_group_by 这一属性,所以导致了错误。

将sql修改成如下的样子后就行了。在 group by 后添加 select中的属性 weight

@Select("SELECT name, SUM(feed_Weight) feedWeight,weight FROM aquaculture where name <= #{name} and id_patch = #{idPatch}  group by name ,weight")
    List<FeedWeight> feedWeight(@Param("name") int name, @Param("idPatch") String idPatch) ;

当然属性名 name 这个不太正规的字段也可能导致问题出现的原因 ,望高手指正。

猜你喜欢

转载自blog.csdn.net/weixin_35040169/article/details/79943703
今日推荐