在准备开发博客系统的前期,观摩了一位大佬的源码:http://pan.baidu.com/s/1eRfrsAm
这是16年的一份博客源码系统,但是在测试部署时发现了一个问题不解!!!
后网上资源解析说与Group by分组条件相关,本人对数据库mysql也是有所学习,但是在这种分组与排序中经常被难倒,于是乎写下这篇,做为记录
idea:控制台server界面输出:
十月 16, 2019 10:29:55 下午 org.apache.catalina.loader.WebappClassLoaderBase clearReferencesJdbc
严重: The web application [/Blog] registered the JDBC driver [com.alibaba.druid.proxy.DruidDriver] but failed to unregister it when the web application was stopped. To prevent a memory leak, the JDBC Driver has been forcibly unregistered.
十月 16, 2019 10:29:55 下午 org.apache.catalina.loader.WebappClassLoaderBase clearReferencesJdbc
严重: The web application [/Blog] registered the JDBC driver [com.mysql.jdbc.Driver] but failed to unregister it when the web application was stopped. To prevent a memory leak, the JDBC Driver has been forcibly unregistered.
[2019-10-16 10:29:55,151] Artifact Blog:war exploded: Error during artifact deployment. See server log for details.
十月 16, 2019 10:30:00 下午 org.apache.catalina.startup.HostConfig deployDirectory
信息: Deploying web application directory C:\Tomcat\apache-tomcat-7.0.67\webapps\manager
十月 16, 2019 10:30:00 下午 org.apache.catalina.startup.HostConfig deployDirectory
信息: Deployment of web application directory C:\Tomcat\apache-tomcat-7.0.67\webapps\manager has finished in 86 ms

tomcat log输出:
org.springframework.jdbc.BadSqlGrammarException:

Error querying database. Cause: com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException: Expression #1 of SELECT list is not in GROUP BY clause and contains nonaggregated column 'db_blog.t2.id' which is not functionally dependent on columns in GROUP BY clause; this is incompatible with sql_mode=only_full_group_by

The error may exist in file Blog\target\Blog\WEB-INF\classes\com\java1234\mappers\BlogTypeMapper.xml]

The error may involve defaultParameterMap

The error occurred while setting parameters

SQL: SELECT t2.id,t2.typeName,COUNT(t1.id) AS blogCount FROM tblog t1 RIGHT JOIN tblogType t2 ON t1.typeId=t2.id GROUP BY t2.typeName order by t2.orderNo;

Cause: com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException: Expression #1 of SELECT list is not in GROUP BY clause and contains nonaggregated column 'db_blog.t2.id' 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 com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException: Expression #1 of SELECT list is not in GROUP BY clause and contains nonaggregated column 'db_blog.t2.id' which is not functionally dependent on columns in GROUP BY clause; this is incompatible with sql_mode=only_full_group_by
其实这边idea已经很明显的指出了是分组带来的错误,在岁连表查询过程中的t2名称表的id查询出错
正规解释:db_blog.t2.id 在 GROUP BY 中违背了 mysql 的规则。
select 字段必须都在 group by 分组条件内(含有函数的字段除外)。(如果遇到 order by 也出现这个问题,同理,order by 字段也都要在group by内 )

于是乎, GROUP BY 中加入 t2.id,成功解决问题。

SELECT
t2.id,
t2.typeName,
COUNT( t1.id ) AS blogCount
FROM
t_blog t1
RIGHT JOIN t_blogtype t2 ON t1.typeId = t2.id
GROUP BY
t2.typeName,
t2.id
ORDER BY
t2.orderNo;

如果这些还没解析明白,可以去https://blog.csdn.net/u010429286/article/details/64444271 看看