mysql中SELECT list is not in GROUP BY clause and contains nonaggregated column解决办法

mysql中SELECT list is not in GROUP BY clause and contains nonaggregated column解决办法

码农世界 2024-05-22 后端 62 次浏览 0个评论

1.说明

报错原因是查询语句中与分组字段不匹配,查询的必须作为全部的分组字段

2.暂时解决

执行如下sql可以暂时解决,重启后失效

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

3.修改配置文件

修改mysql配置文件,mysqld:下面加

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

4.如果是阿里云数据库,修改参数

如果是阿里云数据库出现SELECT list is not in GROUP BY clause and contains nonaggregated column解决

修改这个参数sql_mode,把only开头的设置删除

mysql中SELECT list is not in GROUP BY clause and contains nonaggregated column解决办法

5.修改sql

大致思路是先查询出每天最新的数据,然后分组,后面通过连接查询

SELECT t1.id as id,t1.data as data,t1.time_type as timeType ,DATE_FORMAT(t1.gmt_create, '%Y-%m-%d'  ) as date
FROM ra_us_record AS t1
RIGHT JOIN (
    SELECT MAX(gmt_create) AS max_date
    FROM ra_us_record
		WHERE user_id = 88898 and DATE_FORMAT( gmt_create, '%Y-%m' ) = '2023-04'
    GROUP BY DATE_FORMAT( gmt_create, '%Y-%m-%d' )
) AS t2 ON t1.gmt_create = t2.max_date
ORDER BY t1.gmt_create desc

mysql中SELECT list is not in GROUP BY clause and contains nonaggregated column解决办法

转载请注明来自码农世界,本文标题:《mysql中SELECT list is not in GROUP BY clause and contains nonaggregated column解决办法》

百度分享代码,如果开启HTTPS请参考李洋个人博客
每一天,每一秒,你所做的决定都会改变你的人生!

发表评论

快捷回复:

评论列表 (暂无评论,62人围观)参与讨论

还没有评论,来说两句吧...

Top