mysql group by 规则_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 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 看看
總結
以上是生活随笔為你收集整理的mysql group by 规则_MySQL 中关于 GROUP BY 的问题的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: mysql5.6修改默认目录_MySQL
- 下一篇: linux cmake编译源码,linu