在mysql5.5升级到5.7 或8.0 ,数据库默认sqlMode 是默认only_full_group_by,导致聚合SQL报错
SQL: select a.deptname,ROUND(sum(a.outdata)*0.0001,2) outPolicy,ROUND(sum(a.innerdata)*0.0001,2) innerPolicy from( ( SELECT (case sd.pid when ‘1067246875800000066’ then sd.id else sd.pid end ) deptid, (case sd.pid when ‘1067246875800000066’ then sd.name else
(select name from sys_dept sd1 where sd1.id=sd.pid )end ) deptname, ‘0’ as innerdata, tio.insured_premium outdata FROM tb_insured_out_policy tiop LEFT JOIN tb_insured_out tio ON tiop.id = tio.policy_id LEFT JOIN sys_user su on tio.creator=su.id LEFT JOIN sys_dept sd on sd.id=su.dept_id WHERE tiop.state = ‘1’ AND tiop.create_date > YEAR (NOW()) and tio.insured_premium is not null AND
(SELECT count(0) FROM act_hi_taskinst WHERE PROC_INST_ID_ = tiop.instance_id AND TASK_DEF_KEY_ = ‘review_check’) > 0 ) UNION ( select (case sd.pid when ‘1067246875800000066’ then sd.id else sd.pid end ) deptid, (case sd.pid when ‘1067246875800000066’ then sd.name else (select name from sys_dept sd1 where sd1.id=sd.pid )end ) deptname, sum(tpi.premium) innerdata,‘0’ as outdata from tb_policy_info tpi
LEFT JOIN tb_policy_needs tpn on tpn.id=tpi.needs_id LEFT JOIN sys_user su on tpn.creator=su.id LEFT JOIN sys_dept sd on sd.id=su.dept_id
WHERE tpn.create_date >YEAR (NOW()) and tpi.is_effect=‘2’ GROUP BY tpn.creator ) ) a GROUP BY a.deptid
Cause: java.sql.SQLSyntaxErrorException: Expression #1 of SELECT list is not in GROUP BY clause and contains nonaggregated column ‘a.deptname’ which is not functionally dependent on columns in GROUP BY clause; this is incompatible with sql_mode=only_full_group_by
; bad SQL grammar []; nested exception is java.sql.SQLSyntaxErrorException: Expression #1 of SELECT list is not in GROUP BY clause and contains nonaggregated column ‘a.deptname’ which is not functionally dependent on columns in GROUP BY clause; this is incompatible with sql_mode=only_full_group_by
解决办法
修改配置文件my.cnf
找到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