当前位置:网站首页>mysql查询group by 1055 问题完美解决,最简单最便捷的方法
mysql查询group by 1055 问题完美解决,最简单最便捷的方法
2022-06-22 07:37:00 【努力的蚂蚁【你若】】
错误信息
1055 - Expression #1 of SELECT list is not in GROUP BY clause and contains nonaggregated column ‘test_2.t_score.id’ which is not functionally dependent on columns in GROUP BY clause; this is incompatible with sql_mode=only_full_group_by
时间: 0s
示例
CREATE TABLE `t_score` (
`id` int NOT NULL AUTO_INCREMENT,
`score` int DEFAULT NULL COMMENT '分数',
`create_time` varchar(20) COLLATE utf8mb4_bin DEFAULT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=8 DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_bin;
需求:我有一张打分表,我想按天查询出最高分数的那条数据,包括id
示例数据:
INSERT INTO `t_score` VALUES (1, 5, '2021-09-15');
INSERT INTO `t_score` VALUES (2, 10, '2021-09-15');
INSERT INTO `t_score` VALUES (3, 10, '2021-09-15');
INSERT INTO `t_score` VALUES (4, 15, '2021-09-15');
INSERT INTO `t_score` VALUES (5, 10, '2021-09-16');
INSERT INTO `t_score` VALUES (6, 20, '2021-09-16');
INSERT INTO `t_score` VALUES (7, 15, '2021-09-16');
于是写sql
SELECT
create_time,max( score )
FROM
t_score
GROUP BY
create_time
![[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-t9hhc2OQ-1631783129219)(access/2021-09-16/1.jpg)]](/img/dd/1c38536537a233b5312551e42f2b3f.png)
如果按天查询出最高成绩,没问题。我的需求是需要把id也查询出来,于是:
![[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-WpL60MyG-1631783129221)(access/2021-09-16/2.jpg)]](/img/9a/60ce5aecd58f92347de40e05112a32.png)
报错:错误代码是1055,查询的列表参数1不在group by 子句中,并且包含非聚合列这个表的id;与sql_mode=only_full_group_by不兼容。
原因
这个错误是mysql 5.7.5 版本及以上版本会出现的问题,mysql 5.7.5版本以上默认的sql配置是:sql_mode=“ONLY_FULL_GROUP_BY”,这个配置严格执行了"SQL92标准"。
很多从5.6升级到5.7时,为了语法兼容,大部分都会选择调整sql_mode,使其保持跟5.6一致,为了尽量兼容程序。
由于开启了ONLY_FULL_GROUP_BY的设置,如果select 的字段不在 group by 中,
并且select 的字段未使用聚合函数(SUM,AVG,MAX,MIN等)的话,那么这条sql查询是被mysql认为非法的,会报错误…
如何解决
查询MySQL版本
![[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-v5pEZJY6-1631783199626)(access/2021-09-16/3.jpg)]](/img/2c/6c251d30974448d605ed10888b7718.png)
版本高于5.7.5,所以不兼容。
换一个数据库版本
![[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-vG4kWOBV-1631783199628)(access\2021-09-16\版本5.6.16.jpg)]](/img/8e/1dc5eaadeaec99e86e28eb1f7b3cc2.png)
低于5.7.5,所以可以查询使用。

那总不能直接更换数据库版本吧,有没有解决方案呢。答案:有。
查询sql_mode
select @@global.sql_mode;
![[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-25GnbfEy-1631783256779)(access/2021-09-16/查询sql_mode结果.jpg)]](/img/e2/6314c9b351e0163947d1dde8362520.png)
去掉ONLY_FULL_GROUP_BY即可
解决
windows修改
因为我的是免安装的MySQL8版本
![[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-9g9wXRjz-1631783256781)(access/2021-09-16/免安装mysql8.jpg)]](/img/3c/aabcc35abf497d6234e1428b196a5b.png)
没有这个文件,在安装目录下新建一个my.ini
![[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-3T7myd5T-1631783256784)(access/2021-09-16/添加my.ini文件.jpg)]](/img/8b/cc5f417ecd2cceda277a8d8d68a708.png)
内容:
[mysqld]
sql_mode=STRICT_TRANS_TABLES,NO_ZERO_IN_DATE,NO_ZERO_DATE,ERROR_FOR_DIVISION_BY_ZERO,NO_ENGINE_SUBSTITUTION
linux修改
vim /etc/my.cnf
最后面增加
sql_mode=STRICT_TRANS_TABLES,NO_ZERO_IN_DATE,NO_ZERO_DATE,ERROR_FOR_DIVISION_BY_ZERO,NO_ENGINE_SUBSTITUTION
重启mysql服务
service mysqld restart
再次查询,完成。
![[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-Bu4uobUB-1631783256785)(access\2021-09-16/再次查询.jpg)]](/img/4b/ec84a7d2730404672c9174f3ce9bae.png)
边栏推荐
- Use multithreading to speed up your crawler
- Selenium anti crawl and analog mobile browser
- 8、 Slider assembly
- Open source open source version - pintuan
- Microsoft Remote Desktop 10.7.6正式版
- 对于mysql中数据为NULL引发的一些问题和思考
- Open version - account information synchronization and unification
- Color matching of architecture diagram
- Solution to the problem of "brand abuse such as brand inconsistency and stacking in the published product information" prompted by copying and uploading
- Shutter margin negative margin
猜你喜欢

How to batch copy babies by sales volume in Taoying

Wx applet vant UI call interface to realize two-level cascade

Detailed explanation of capturing the whole store treasure and uploading it to multiple stores

How to backup the treasures in the store and upload them to multiple stores

Canvastotempfilepath of wechat

Get through version 4.3 mind map

Reasons and solutions for Taobao's baby's prompt "sales attribute is required and parameter format is wrong"

Crmeb mall distribution function

微信小游戏(四)

Backup the method of uploading babies in Taobao stores to multiple stores
随机推荐
Wechat applet service provider sub merchant payment order interface
Crmeb open source version, a free mall worth trying
Semaphore
vue连接mysql数据库失败
Solution to the problem of "brand abuse such as brand inconsistency and stacking in the published product information" prompted by copying and uploading
The applet uses the step bar vant steps in vant
Backup the method of uploading babies in Taobao stores to multiple stores
Detailed explanation of capturing the whole store treasure and uploading it to multiple stores
Blob format problems involved in image uploading
New year's Day mall material Icon
FileTool
Crmeb mall order shipping function
Baidu Post Bar crawler crawls to the middle of the building
ASP. Net core development experience
微信小游戏(四)
网站是否要修改标题
各大企业连连出现亏损,环保行业何去何从?
如何解决‘WebDriver‘ object has no attribute ‘switch_to_window‘
Cocoapods problem record
代码覆盖率测试对编程小白的意义及其使用方法