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'
相关推荐
苏瞳儿1 天前
java对数据库的增删改查
java·数据库·oracle
Greyson11 天前
Layui表格如何使用第三方插件实现树形展示.txt
jvm·数据库·python
2401_871696521 天前
mysql行级锁失效的原因排查_检查查询条件与执行计划
jvm·数据库·python
Elastic 中国社区官方博客1 天前
Elasticsearch:快速近似 ES|QL - 第一部分
大数据·运维·数据库·elasticsearch·搜索引擎·全文检索
Dontla1 天前
高基数(High Cardinality)问题介绍(Prometheus、高基数字段、低基数字段)
前端·数据库·prometheus
a9511416421 天前
CSS如何实现元素隐藏不占位_使用display-none完全移除
jvm·数据库·python
SelectDB技术团队1 天前
SelectDB Enterprise 4.0.5:强化安全与治理,构建企业级实时分析与 AI 数据底座
数据库·人工智能·apache doris
一 乐1 天前
医院挂号|基于springboot + vue医院挂号管理系统(源码+数据库+文档)
java·数据库·vue.js·spring boot·论文·毕设·医院挂号管理系统
ego.iblacat1 天前
Redis 核心概念与部署
数据库·redis·缓存
m0_493934531 天前
如何监控AWR数据收集Job_DBA_SCHEDULER_JOBS中的BSLN_MAINTAIN_STATS
jvm·数据库·python