mybatis解决查询中使用group by时出现sql_mode=only_full_group_by

mybatis解决查询中使用group by时出现sql_mode=only_full_group_by问题

错误异常
sql 复制代码
select 列1,列2,列3 from 表名 group by 列1

以上查询语句

使用group对列1进行分组查询,单列2和列3并没有分组这时候系统会出现以下错误信息

Cause: java.sql.SQLSyntaxErrorException: Expression #11 of SELECT list is not in GROUP BY clause and contains nonaggregated column '***' 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 java.sql.SQLSyntaxErrorException: Expression #11 of SELECT list is not in GROUP BY clause and contains nonaggregated column '*****' which is not functionally dependent on columns in GROUP BY clause; this is incompatible with sql_mode=only_full_group_by

异常原因

这里原因是因为mysql版本或配置不支持分组查询时查询分组以外的列,导致异常

解决方案

解决方案1

在未进行分组查询的列中增加ANY_VALUE()关键词解决

例如: select 列1,ANY_VALUE(列2),ANY_VALUE(列3) from 表名 group by 列1
解决方案2

在datasource配置后增加&sessionVariables=sql_mode='STRICT_TRANS_TABLES,NO_ZERO_IN_DATE,NO_ZERO_DATE,ERROR_FOR_DIVISION_BY_ZERO,NO_AUTO_CREATE_USER,NO_ENGINE_SUBSTITUTION'

config 复制代码
# 例如
 spring.datasource.url=jdbc:mysql://localhost:3306/数据库名....&sessionVariables=sql_mode='STRICT_TRANS_TABLES,NO_ZERO_IN_DATE,NO_ZERO_DATE,ERROR_FOR_DIVISION_BY_ZERO,NO_AUTO_CREATE_USER,NO_ENGINE_SUBSTITUTION'
相关推荐
上海安当技术3 分钟前
统一身份认证平台怎么落地?11 个异构业务系统接入 ASP 的完整实施路径
数据库·servlet·架构·kubernetes·jenkins
在水一缸1 小时前
PGSimCity:当数据库内核变成一座可以漫步的城市
数据库·postgresql·可视化·开源项目·数据库内核·pgsimcity·技术科普
yio_yin1 小时前
MyBatis
java·前端·mybatis
jnrjian1 小时前
psql 执行多个 sql 文件
数据库·sql
無a伟2 小时前
Redis事务详解:原理、实战、坑点与实践
mybatis
@insist1233 小时前
信息系统管理工程师-运维人员管理与核心运维过程(上篇)
数据库·软考·软件水平考试·信息系统管理工程师·软考信管
muddjsv4 小时前
SQLite 零基础 CRUD 实战详解:增删改查标准语法与高危避坑
数据库·sqlite
豆角焖肉4 小时前
MyBatis延迟加载、缓存机制与注解开发
java·spring·mybatis
~光~~5 小时前
【嵌入式linux学习_OV8858 bring up】L2_V4L2与Ov8858
linux·数据库·学习
菜地里的小菜鸟5 小时前
达梦数据库备份与恢复
数据库·达梦·达梦数据库备份与恢复