当前位置: 首页 > news >正文

【Java报错】数据库查询报错this is incompatible with sql_mode=only_full_group_by

报错信息

完整报错信息如图
在这里插入图片描述

Error querying database. Cause: java.sql.SQLSyntaxErrorException: Expression #1 of ORDER BY clause is not in GROUP BY clause and contains nonaggregated column 'lst_as.t.create_time' which is not functionally dependent on columns in GROUP BY clause; this is incompatible with sql_mode=only_full_group_by ### The error may exist in URL [jar:file:/data/wxjz/version/run/linjiashop-admin-api-as-1.0.jar!/BOOT-INF/lib/linjiashop-core-1.0.jar!/com/jiexun/user/mapper/RcustomerAccountInfo.xml] ### The error may involve defaultParameterMap ### The error occurred while setting parameters ### SQL: SELECT DATE_FORMAT(t.create_time, '%Y-%m') AS date, SUM(t.wait_repayment/100) AS waitingPay FROM t_customer_account_info t WHERE t.create_time = ( SELECT MAX(t2.create_time) FROM t_customer_account_info t2 WHERE t2.account_id = t.account_id AND YEAR(t2.create_time) = YEAR(t.create_time) AND MONTH(t2.create_time) = MONTH(t.create_time) ) AND t.tenant_id = ? GROUP BY DATE_FORMAT(t.create_time, '%Y-%m') ORDER BY t.create_time; ### Cause: java.sql.SQLSyntaxErrorException: Expression #1 of ORDER BY clause is not in GROUP BY clause and contains nonaggregated column 'lst_as.t.create_time' 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 #1 of ORDER BY clause is not in GROUP BY clause and contains nonaggregated column 'lst_as.t.create_time' which is not functionally dependent on columns in GROUP BY clause; this is incompatible with sql_mode=only_full_group_by

报错SQL语句

   SELECT DATE_FORMAT(t.create_time, '%Y-%m') AS date, SUM(t.wait_repayment/100) AS waitingPay FROM t_customer_account_info t WHERE t.create_time = ( SELECT MAX(t2.create_time) FROM t_customer_account_info t2 WHERE t2.account_id = t.account_id AND YEAR(t2.create_time) = YEAR(t.create_time) AND MONTH(t2.create_time) = MONTH(t.create_time) ) AND t.tenant_id = ? GROUP BY DATE_FORMAT(t.create_time, '%Y-%m') ORDER BY t.create_time;

错误原因

这个错误是由于MySQL的ONLY_FULL_GROUP_BY模式导致的。在ONLY_FULL_GROUP_BY模式下,SQL查询中的ORDER BY子句中的列必须出现在GROUP BY子句中,或者在聚合函数中使用。
如上述SQL,在SQL查询中,ORDER BY t.create_time中的t.create_time没有出现在GROUP BY子句中,因此导致了这个错误。


解决方法

1.修改SQL查询

ORDER BY子句中的列改为聚合函数或添加到GROUP BY子句中。
可以将ORDER BY子句中的t.create_time改为ORDER BY date,因为date已经在GROUP BY子句中。修改后的SQL查询如下:

   SELECT DATE_FORMAT(t.create_time, '%Y-%m') AS date, SUM(t.wait_repayment/100) AS waitingPay FROM t_customer_account_info t WHERE t.create_time = ( SELECT MAX(t2.create_time) FROM t_customer_account_info t2 WHERE t2.account_id = t.account_id AND YEAR(t2.create_time) = YEAR(t.create_time) AND MONTH(t2.create_time) = MONTH(t.create_time) ) AND t.tenant_id = ? GROUP BY DATE_FORMAT(t.create_time, '%Y-%m') ORDER BY date;

2.修改MySQL配置

2.1 通过SQL命令临时修改(重启后失效)

如果你暂时不想修改配置文件,可以使用SQL命令临时修改sql_mode。但请注意,这种方法在MySQL重启后会失效。

   SET GLOBAL sql_mode=(SELECT REPLACE(@@sql_mode,'ONLY_FULL_GROUP_BY',''));

2.2 修改MySQL配置文件(永久生效,推荐)

首先找到MySQL配置文件,MySQL的配置文件通常是my.cnf(在Linux上)或my.ini(在Windows上)。你可以通过以下命令找到配置文件的位置:

     mysql --help | grep my.cnf

然后编辑配置文件:

  • 打开配置文件,找到或添加[mysqld]部分。
  • 修改或添加sql_mode配置,移除ONLY_FULL_GROUP_BY

例如,修改后的配置文件内容如下:
在这里插入图片描述

最后保存并重启MySQL服务,在Linux上,你可以使用以下命令重启MySQL服务

   sudo systemctl restart mysql

在Windows上,你可以通过服务管理器(通过运行services.msc打开)重启MySQL服务


至此就解决了以上报错问题

http://www.xdnf.cn/news/9775.html

相关文章:

  • MAC-如果是分页查询,怎么分批次in;基于多线程的分页查询工具类
  • 492Q 型气缸盖双端面铣削组合铣床总体设计
  • Jenkins 多分支流水线: 如何创建用于 Jenkins 状态检查的 GitHub 应用
  • vue3学习笔记之条件渲染
  • 「数据可视化 D3系列」入门第八章:动画效果详解(让图表动起来)
  • 「GitHub热榜」AIGC系统源码:AI问答+绘画+PPT+音乐生成一站式
  • 第12篇:Linux程序访问控制FPGA端Switch<一>
  • Yocto项目实战教程 · 第4章:4.2小节-菜谱
  • AWS Elastic Beanstalk的部署Python Flask后端服务(Hello,World)
  • JavaScript 性能优化实战
  • 决战浏览器渲染:减少重绘(Repaint)与重排(Reflow)的性能优化策略
  • 基于springBoot+vue的PC 端学习系统(源码+lw+部署文档+讲解),源码可白嫖!
  • C++ AVL树
  • HAL库通过FATFS和SDIO+DMA写入SD卡数据错误
  • 2025MathorcupD题 短途运输货量预测及车辆调度问题 保姆级教程讲解|模型讲解
  • L2-006 树的遍历
  • DHTMLX宣布推出支持 Redux、TypeScript 和 MUI 的 React Gantt甘特图控件
  • redis利用备忘录
  • Jsp技术入门指南【五】详细讲解jsp结构页面
  • 【含文档+PPT+源码】基于Python爬虫二手房价格预测与可视化系统的设计与实现
  • 论文阅读:2024 arxiv AI Safety in Generative AI Large Language Models: A Survey
  • 自然语言处理入门7——注意力机制
  • 数据结构——顺序表(C语言实现)
  • [250418] 智谱 AI 发布新一代模型,同时推出新域名 Z.ai
  • yocto编译使用共享缓存
  • IntelliSense 已完成初始化,但在尝试加载文档时出错
  • 前端单元测试实战:如何开始?
  • Vue2+Vue3 130~180集学习笔记
  • Google Colab测试部署Qwen大模型,实现PDF转MD场景OCR 识别(支持单机环境)
  • 迭代器模式:统一不同数据结构的遍历方式