MySQL 中關於 GROUP BY 的問題

在準備開發博客系統的前期,觀摩了一位大佬的源碼: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 msjava

tomcat log輸出:
org.springframework.jdbc.BadSqlGrammarException: mysql

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內 )web

因而乎, GROUP BY 中加入 t2.id,成功解決問題。spring

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;sql

若是這些還沒解析明白,能夠去https://blog.csdn.net/u010429286/article/details/64444271 看看數據庫

相關文章
相關標籤/搜索