mysql 5.7版本异常Expression #1 of ORDER BY clause is not in GROUP BY clause and contains 处理

今天在做按时间分组统计数据时,一段sql在数据库运行正常,但是在java代码中抛出了异常:
sql语句:

SELECT DATE_FORMAT( commitTime, '%H' ) AS gjtime , COUNT(1) AS gjsum FROM jenkins_data GROUP BY gjtime ORDER BY commitTime DESC

java 抛出的异常信息:

Expression #1 of ORDER BY clause is not in GROUP BY clause and contains nonaggregated column

不明所以,网上也给出了很多的处理方法,修改my.cnf,这类的居多,但也从中看到一篇文章,他直接把ORDER BY commitTime DESC语句更改为ORDER BY MAX(commitTime) DESC
修改后的语句:

SELECT DATE_FORMAT( commitTime, '%H' ) AS gjtime , COUNT(1) AS gjsum FROM jenkins_data GROUP BY gjtime ORDER BY MAX(commitTime) DESC

这样在执行就没有问题了。

你可能感兴趣的:(实用的处理小技巧)