【mysql】 this is incompatible with sql_mode=only_full_group_by 修改数据库的默认sql-mode,去除 only_full_group_by

目录

    • 事件起因
    • 环境和工具
    • 解决办法
      • 临时解决办法
      • 永久的解决办法
    • 总结:
    • 结束语

事件起因

在重新安装了数据库后,运行之前的一个项目,在首页的查询遇到了这么一个问题,报错信息如下所示:
【mysql】 this is incompatible with sql_mode=only_full_group_by 修改数据库的默认sql-mode,去除 only_full_group_by_第1张图片

详细信息如下所示:

xxx on columns in GROUP BY clause; this is incompatible with sql_mode=only_full_group_by

系统异常请稍后再试 ### Error querying database. Cause: java.sql.SQLSyntaxErrorException: Expression #1 of SELECT list is not in GROUP BY clause and contains nonaggregated column 'jiangxi_vr.t.station_id' 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 file [D:\desktop\job_project\jiangxi-nanchang\jiangxi-nancang-yidong-vr-backend\target\classes\mapper\pc\BusinessStationMapper.xml] ### The error may involve defaultParameterMap ### The error occurred while setting parameters ### SQL: select t.*, count(bd.device_id) as deviceTotal from business_station t left join business_device bd on t.station_id = bd.station_id where t.del_flag=2 and t.area in ( ? , ? , ? , ? , ? , ? ) and fun_distance(station_dimension,?) <= ? group by station_no order by fun_distance(station_dimension,?) ### Cause: java.sql.SQLSyntaxErrorException: Expression #1 of SELECT list is not in GROUP BY clause and contains nonaggregated column 'jiangxi_vr.t.station_id' 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 SELECT list is not in GROUP BY clause and contains nonaggregated column 'jiangxi_vr.t.station_id' which is not functionally dependent on columns in GROUP BY clause; this is incompatible with sql_mode=only_full_group_by

其实这里大致的错误原因是:
这个错误是由于MySQL的only_full_group_by SQL模式引起的。在only_full_group_by模式下,当使用GROUP BY语句时,SELECT列表中的字段必须是GROUP BY子句中的聚合函数或者在GROUP BY子句中出现的列。如果SELECT列表中包含了未聚合的列并且不在GROUP BY子句中,就会出现这个错误。

环境和工具

mysql5.7.36
idea2022.3

解决办法

临时解决办法

使用下面两条sql语句 可以在navicat里面 用于查询当前的sql_mode 和修改当前的sql_mode

-- 查看当前的sql_mode:
SHOW VARIABLES LIKE 'sql_mode';
--修改当前的sql_mode 去掉only_full_group_by的模式:
SET sql_mode=(SELECT REPLACE(@@sql_mode,'ONLY_FULL_GROUP_BY',''));

永久的解决办法

在系统的c盘目录下:
找到programData(注意该文件夹为隐藏文件夹,需要勾选显示隐藏的项目)
比如我的路径就如下所示:(我安装时选择的路径是d盘我自己的路径,但是这个文件始终是在c盘下的)
C:\ProgramData\MySQL\MySQL Server 5.7\my.ini
(有的文件名叫my.cnf)
【mysql】 this is incompatible with sql_mode=only_full_group_by 修改数据库的默认sql-mode,去除 only_full_group_by_第2张图片
打开my.ini 去除only_full_group_by 然后重启mysql的服务即可,据说有的mysql版本是my.cnf文件,但里面的配置是相同的
【mysql】 this is incompatible with sql_mode=only_full_group_by 修改数据库的默认sql-mode,去除 only_full_group_by_第3张图片
打开任务管理器Ctrl + shift + esc 选中任务,然后重启服务即可
【mysql】 this is incompatible with sql_mode=only_full_group_by 修改数据库的默认sql-mode,去除 only_full_group_by_第4张图片

总结:

在我写这篇文章的时候,旁边一位吨位比较大的大佬,看到了我写的内容,顺便跟我交流了一下:按道理我们不应该第一步去尝试关闭这个配置,而是应该去改造这个sql,下面是他说的一些话,我听了后也觉得挺有道理的:

其实不建议这么去做,为了达到交付和集成的效率,必须保证开发环境和测试环境和正式环境要相同,以此提高整个交付部署项目的高效性,按道理我们测试环境和正式环境的数据库开放了这个only_full_group_by的话,我们开发环境应该与正式环境保持一致以保证项目能够实现快速部署,所以其实遇到这个问题,不应该首先去修改本地的mysql这个配置,而是应该改造sql去避免这种情况,因为现在mysql的数据库默认都是开启only_full_group_by这个功能的,写sql时应该极力避免这种情况的发生

结束语

若是对你有所帮助的话,希望能获得你的 点赞、评论、收藏,这将是对我很大的鼓励!!! 这对我真的很重要!!!
蟹蟹٩(‘ω’)و

你可能感兴趣的:(IT成长笔记,大后端开发学习,数据库,数据库,mysql,sql)