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'
相关推荐
多巴胺梦想家4 小时前
事务与并发控制:当多人同时操作数据库
服务器·数据库·oracle
howard20054 小时前
PostgreSQL起步
数据库·postgresql
秋田君5 小时前
QT_QT布局详解
开发语言·数据库·qt
从此以后自律5 小时前
MyBatis-Plus 内置分页、PageHelper 自定义分页
mybatis
可乐ea6 小时前
【Redis八股|第8篇】Redis 分布式锁原理与 Redisson 使用
数据库·redis·分布式·面试题·redis八股
ClouGence6 小时前
Oracle 到 OceanBase 迁移方案横评:停机导出/导入 vs OMS vs CDC 工具
数据库·oracle
Meya11276 小时前
不同规模机房怎么选 U 位系统?8 柜小型机房、40 柜数据中心完整选型参考
服务器·网络·数据库
4154117 小时前
MyBatis-Plus + PostGIS 实战(1.1):Geometry 字段在 Swagger 中的优雅展示
java·mybatis·postgis
从此以后自律8 小时前
MySQL 删除数据全方式详解
数据库·mysql
青山木8 小时前
Redis 高可用的最后一公里:Cluster 分片、Gossip 与故障转移全流程
数据库·redis·后端·缓存