1055 - Expression #1 of SELECT list is not in GROUP BY clause and contains nonaggregated column 'cre
·
sql语句
SELECT * FROM t_company_change_log WHERE company_id='257949626441666560' AND rec_status='N' GROUP BY change_item
这样会在某些版本的数据库中运行出错
原因是 5.7.5之后版本默认启用了only_full_group_by SQL模式,所以MySQL会不识别选择列表、条件或顺序列表引用的查询 ,
1055 - Expression #1 of SELECT list is not in GROUP BY clause and contains nonaggregated column 'creationlink_pro_db.t_company_change_log.id' which is not functionally dependent on columns in GROUP BY clause; this is incompatible with sql_mode=only_full_group_by, Time: 0.000000s
错误翻译过来就是:
1055-SELECT列表的表达式#1不在GROUP BY子句中,并且包含未聚合的列’creationlink_pro_db.t_company_change_log.id’,该列在功能上不依赖于GROUP BY子句中的列; 这与sql_mode = only_full_group_by不兼容,时间:0.000000s
改正之后的写法:
SELECT change_item FROM t_company_change_log WHERE company_id='257949626441666560' AND rec_status='N' GROUP BY change_item
但是,下面查询字段加了两个GROUP BY没有的字段id和company_id,这样运行是会报错的
SELECT id,company_id,change_item FROM t_company_change_log WHERE company_id='257949626441666560' AND rec_status='N' GROUP BY change_item
根本解决办法关闭only_full_group_by SQL模式
参考地址
总结:写select查询是,不要用*查询所有,这样不仅拖慢查询速度,还可能遇到莫名奇妙的错,就跟我这次一样报1055
更多推荐
已为社区贡献2条内容
所有评论(0)