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'
相关推荐
倔强的石头_12 小时前
kingbase备份与恢复实战(二)—— sys_dump库级逻辑备份与恢复(Windows详细步骤)
数据库
jiayou642 天前
KingbaseES 实战:深度解析数据库对象访问权限管理
数据库
李广坤3 天前
MySQL 大表字段变更实践(改名 + 改类型 + 改长度)
数据库
爱可生开源社区4 天前
2026 年,优秀的 DBA 需要具备哪些素质?
数据库·人工智能·dba
随逸1774 天前
《从零搭建NestJS项目》
数据库·typescript
加号34 天前
windows系统下mysql多源数据库同步部署
数据库·windows·mysql
シ風箏4 天前
MySQL【部署 04】Docker部署 MySQL8.0.32 版本(网盘镜像及启动命令分享)
数据库·mysql·docker
李慕婉学姐4 天前
Springboot智慧社区系统设计与开发6n99s526(程序+源码+数据库+调试部署+开发环境)带论文文档1万字以上,文末可获取,系统界面在最后面。
数据库·spring boot·后端
百锦再4 天前
Django实现接口token检测的实现方案
数据库·python·django·sqlite·flask·fastapi·pip
tryCbest4 天前
数据库SQL学习
数据库·sql