Expression #1 of SELECT list is not in GROUP BY clause and contains nonaggregated column【bug】解决方案

问题场景:

select stuid from student_table GROUP BY stuAccount HAVING count(stuAccount)>1;

运行完这条sql语句后mysql就报了这个错误。

Expression #1 of SELECT list is not in GROUP BY clause and contains nonaggregated column…


解决方案:

1.对于已经存在的数据库

执行下面的sql语句即可(没有效果重启数据库服务)

SET sql_mode ='STRICT_TRANS_TABLES,NO_ZERO_IN_DATE,NO_ZERO_DATE,ERROR_FOR_DIVISION_BY_ZERO,NO_ENGINE_SUBSTITUTION';

2.对于新建的数据库

执行下面的sql语句即可(没有效果重启数据库服务)

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

3.修改SQL语句

我在stack overflow上找到了高赞回答。
When MySQL’s only_full_group_by mode is turned on, it means that strict ANSI SQL rules will apply when using GROUP BY. With regard to your query, this means that if you GROUP BY of the proof_type column, then you can only select two things:

  • the proof_type column,
  • or aggregates of any other column

简单翻译过来就是在only_full_group_by这种模式下,使用GROUP BY只能查询proof_type列和使用聚合函数的列。聚合函数如MIN(), MAX(), or AVG()。
于是我在stuid属性上加max()就可以了。

select max(stuid) from student_table GROUP BY stuAccount HAVING count(stuAccount)>1;

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
引用\[1\]、\[2\]和\[3\]都是关于MySQL报错的信息,其中提到了"Expression #1 of SELECT list is not in GROUP BY clause and contains nonaggregated column"的错误。这个错误是由于在使用GROUP BY子句进行分组查询时,SELECT列表中的某些列没有在GROUP BY子句中出现,并且这些列也没有被聚合函数包含,导致不兼容的错误。这个错误通常是由于MySQLsql_mode设置为only_full_group_by引起的。解决这个错误的方法是要么将SELECT列表中的列添加到GROUP BY子句中,要么将这些列用聚合函数进行处理,以确保查询的正确性。 #### 引用[.reference_title] - *1* *3* [MySQL报错:Expression #1 of SELECT list is not in GROUP BY clause and contains nonaggregated column ...](https://blog.csdn.net/Saintmm/article/details/124491860)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^control_2,239^v4^insert_chatgpt"}} ] [.reference_item] - *2* [Expression #1 of SELECT list is not in GROUP BY clause and contains nonaggre](https://blog.csdn.net/weixin_44051060/article/details/105602832)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^control_2,239^v4^insert_chatgpt"}} ] [.reference_item] [ .reference_list ]

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值