前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >解析SQLSyntaxErrorException异常:not in GROUP BY clause

解析SQLSyntaxErrorException异常:not in GROUP BY clause

作者头像
修己xj
发布2023-08-25 11:20:19
2490
发布2023-08-25 11:20:19
举报
文章被收录于专栏:修己xj修己xj

大家好,欢迎阅读我们的文章。今天,我们将讨论一个常见的Java异常——java.sql.SQLSyntaxErrorException,并深入探讨其中一个具体的错误信息:Expression #1 of SELECT list is not in GROUP BY clause。

异常详情
代码语言:javascript
复制
Caused by: java.sql.SQLSyntaxErrorException: Expression #1 of SELECT list is not in GROUP BY clause and contains nonaggregated column 'cnpc.T1.id' which is not functionally dependent on columns in GROUP BY clause; this is incompatible with sql_mode=only_full_group_by
        at com.mysql.cj.jdbc.exceptions.SQLError.createSQLException(SQLError.java:120)
        at com.mysql.cj.jdbc.exceptions.SQLExceptionsMapping.translateException(SQLExceptionsMapping.java:122)
        at com.mysql.cj.jdbc.ClientPreparedStatement.executeInternal(ClientPreparedStatement.java:916)
        at com.mysql.cj.jdbc.ClientPreparedStatement.execute(ClientPreparedStatement.java:354)
        at com.alibaba.druid.filter.FilterChainImpl.preparedStatement_execute(FilterChainImpl.java:3446)
        at com.alibaba.druid.filter.FilterEventAdapter.preparedStatement_execute(FilterEventAdapter.java:434)
        at com.alibaba.druid.filter.FilterChainImpl.preparedStatement_execute(FilterChainImpl.java:3444)
        at com.alibaba.druid.proxy.jdbc.PreparedStatementProxyImpl.execute(PreparedStatementProxyImpl.java:152)
        at com.alibaba.druid.pool.DruidPooledPreparedStatement.execute(DruidPooledPreparedStatement.java:483)
        at sun.reflect.GeneratedMethodAccessor176.invoke(Unknown Source)
        at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
        at java.lang.reflect.Method.invoke(Method.java:498)
        at org.apache.ibatis.logging.jdbc.PreparedStatementLogger.invoke(PreparedStatementLogger.java:59)
        at com.sun.proxy.$Proxy161.execute(Unknown Source)
        at org.apache.ibatis.executor.statement.PreparedStatementHandler.query(PreparedStatementHandler.java:64)
        at org.apache.ibatis.executor.statement.RoutingStatementHandler.query(RoutingStatementHandler.java:79)
        at org.apache.ibatis.executor.SimpleExecutor.doQuery(SimpleExecutor.java:63)
        at org.apache.ibatis.executor.BaseExecutor.queryFromDatabase(BaseExecutor.java:325)
        at org.apache.ibatis.executor.BaseExecutor.query(BaseExecutor.java:156)
        at org.apache.ibatis.executor.CachingExecutor.query(CachingExecutor.java:109)
        at com.github.pagehelper.util.ExecutorUtil.executeAutoCount(ExecutorUtil.java:169)
        at com.github.pagehelper.PageInterceptor.count(PageInterceptor.java:197)
        at com.github.pagehelper.PageInterceptor.intercept(PageInterceptor.java:140)
        at org.apache.ibatis.plugin.Plugin.invoke(Plugin.java:62)
        at com.sun.proxy.$Proxy238.query(Unknown Source)
        at org.apache.ibatis.session.defaults.DefaultSqlSession.selectList(DefaultSqlSession.java:151)
        at org.apache.ibatis.session.defaults.DefaultSqlSession.selectList(DefaultSqlSession.java:145)
        at org.apache.ibatis.session.defaults.DefaultSqlSession.selectList(DefaultSqlSession.java:140)
        at sun.reflect.GeneratedMethodAccessor304.invoke(Unknown Source)
        at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
        at java.lang.reflect.Method.invoke(Method.java:498)
        at org.mybatis.spring.SqlSessionTemplate$SqlSessionInterceptor.invoke(SqlSessionTemplate.java:427)
        ... 155 common frames omitted
异常说明

这个错误是由于你的SQL查询中的SELECT列表中的表达式不在GROUP BY子句中,并且包含了非聚合列'cnpc.T1.id',这个列在GROUP BY子句中的列上没有函数依赖关系。这与sql_mode=only_full_group_by不兼容。

解决方案

你可以采取以下几种方法来解决这个问题:

  • 在GROUP BY子句中包含所有非聚合列:根据错误信息,你可以尝试在GROUP BY子句中包含'cnpc.T1.id'列,即使它不是聚合列。这样做可以满足only_full_group_by模式的要求。
  • 使用聚合函数:如果你不想在GROUP BY子句中包含'cnpc.T1.id'列,你可以考虑使用聚合函数来处理该列的值。例如,你可以使用MAX()函数获取该列的最大值或使用GROUP_CONCAT()函数将该列的值连接成一个字符串。
  • 修改sql_mode:如果你不需要启用only_full_group_by模式,你可以修改数据库的sql_mode设置,将其更改为允许非聚合列在SELECT列表中。具体的修改方法可能因数据库系统而异,请参考你使用的数据库系统的文档。以下是mysql5.7.33中的示例:
代码语言:javascript
复制
# 查看sql_mode
select @@global.sql_mode;
# 设置sql_mode
set @@global.sql_mode ='STRICT_TRANS_TABLES,NO_ENGINE_SUBSTITUTION';

根据你的具体需求和查询逻辑,选择适合的解决方法来修复这个错误。

本文参与 腾讯云自媒体分享计划,分享自微信公众号。
原始发表:2023-06-22,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 修己xj 微信公众号,前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 异常详情
  • 异常说明
  • 解决方案
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档