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'
相关推荐
p&f°1 天前
PostgreSQL 执行计划控制参数详解
数据库·postgresql·oracle
航Hang*1 天前
第3章:复习篇——第1节:创建和管理数据库
开发语言·数据库·笔记·sql·sqlserver
铉铉这波能秀1 天前
正则表达式从入门到精通(字符串模式匹配)
java·数据库·python·sql·正则表达式·模式匹配·表格处理
sc.溯琛1 天前
MySQL 进阶实验:数据库与数据表管理完全指南
数据库·oracle
好记忆不如烂笔头abc1 天前
oracle迁移到sqlserver的注意点
数据库·oracle·sqlserver
YJlio1 天前
ZoomIt 学习笔记(11.11):休息计时器与演讲节奏控制——倒计时、番茄钟与现场掌控力
数据库·笔记·学习
sc.溯琛1 天前
MySQL 性能优化核心:索引创建与管理实战指南
数据库·mysql·性能优化
2024暴富1 天前
SpringBoot基于Mybatis拦截器实现数据权限(图文)
spring boot·spring cloud·mybatis
锋君1 天前
Orcale数据库在Asp.Net Core环境下使用EF Core 生成实体
数据库·后端·oracle·asp.net
啊吧怪不啊吧1 天前
SQL之用户管理——权限与用户
大数据·数据库·sql