报错,> 1055 - Expression #1 of SELECT list is not in GROUP BY clause and contains nonaggregated column

错误:
mysql:
select * ,count(*)as count from app_cash_trans_log group by app_cash_trans_log.tran_code having count(*)>=(select pla_config.cfg_value from pla_config where pla_config.cfg_name='tranMaxContinuity') order by app_cash_trans_log.id desc limit 5
> 1055 - Expression #1 of SELECT list is not in GROUP BY clause and contains nonaggregated column 'atmp_service.app_cash_trans_log.id' which is not functionally dependent on columns in GROUP BY clause; this is incompatible with sql_mode=only_full_group_by
> 时间: 0.001s
原因:
在MySQL5.7之后,sql_mode中默认存在ONLY_FULL_GROUP_BY,SQL语句未通过ONLY_FULL_GROUP_BY语义检查所以报错。
ONLY_FULL_GROUP_BY:ONLY_FULL_GROUP_BY要求select语句中查询出来的列必须是明确的(其他语句也是一样)。
以SQL语句select columes from table group by list为例:columns必须是聚集函数或者在group by后的表达式list中,并且list中必须包含主键,否则也会报错。
insert、update、delete语句都会报错(但不影响SQL语句的执行),因为这三种语句执行之前也会执行查询操作。
以主键为id的表为例:
SELECT count(1) FROM customer GROUP BY `name`;该SQL执行成功,因为count是聚集函数;
SELECT * FROM customer GROUP BY `name`;该SQL执行失败,因为*中包含主键id,而group by后的表达式中并没有包含id
SELECT name FROM customer GROUP BY `name`;该SQL执行成功,因为name包含在group by后的表达式中
SELECT name, contact FROM customer GROUP BY `name`;该SQL执行失败,因为contact没有包含在group by后的表达式中

解决方法1:
修改mysql配置去掉ONLY_FULL_GROUP_BY
select @@sql_mode
set sql_mode='STRICT_TRANS_TABLES,NO_ZERO_IN_DATE,NO_ZERO_DATE,ERROR_FOR_DIVISION_BY_ZERO,NO_AUTO_CREATE_USER,NO_ENGINE_SUBSTITUTION'

解决方法2:
修改sql语句为
select tran_code,any_value(tran_name)as tran_name ,any_value(tran_date) as tran_date ,any_value(tran_time) as tran_time,any_value(id) as id,count(tran_code)as count 
from app_cash_trans_log 
group by tran_code
having count >=(select pla_config.cfg_value from pla_config where pla_config.cfg_name='tranMaxContinuity') 
order by id desc limit 5;

  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值