1. 程式人生 > >解決MySql查詢語句報MySQLSyntaxErrorException異常的問題

解決MySql查詢語句報MySQLSyntaxErrorException異常的問題

MySQLSyntaxErrorException

日誌

Cause: com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException: Expression #3 of SELECT list is not in GROUP BY clause and contains nonaggregated column 'db_system.tb_blog_article.create_time' 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 #3 of SELECT list is not in GROUP BY clause and contains nonaggregated column 'db_system.tb_blog_article.create_time' which is not functionally dependent on columns in GROUP BY clause; this is incompatible with sql_mode=only_full_group_by] with root cause

com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException: Expression #3 of SELECT list is not in GROUP BY clause and contains nonaggregated column 'db_system.tb_blog_article.create_time' which is not functionally dependent on columns in GROUP BY clause; this is incompatible with sql_mode=only_full_group_by
        at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method) ~[na:1.8.0_91]
        at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:62) ~[na:1.8.0_91]
        at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45) ~[na:1.8.0_91]
        at java.lang.reflect.Constructor.newInstance(Constructor.java:423) ~[na:1.8.0_91]
        at com.mysql.jdbc.Util.handleNewInstance(Util.java:411) ~[mysql-connector-java-5.1.22.jar:na]
        at com.mysql.jdbc.Util.getInstance(Util.java:386) ~[mysql-connector-java-5.1.22.jar:na]
        at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:1053) ~[mysql-connector-java-5.1.22.jar:na]
        at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:4096) ~[mysql-connector-java-5.1.22.jar:na]
        at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:4028) ~[mysql-connector-java-5.1.22.jar:na]

程式碼

mybatis查詢語句如下

 <select id="getMonthWriteCount" resultType="java.util.Map">
        SELECT
            DATE_FORMAT(create_time, '%Y%m') AS "dayTime",
            count(*) AS "dayCount",
            DATE_FORMAT(create_time, '%Y年%m月') as "month"
        FROM
            tb_blog_article
        WHERE
            create_time IS NOT NULL
        GROUP BY
            dayTime
        ORDER BY
            create_time DESC
    <
/select>

問題根源:

MySql版本的問題,在5.7以上的版本實現了對功能依賴的檢測,MySql預設啟用了only_full_group_by SQL模式.

解決方法

進入MySql安裝路徑下,開啟my.ini檔案,centos系統上mysql的檔案是在/etc/my.cnf,在檔案的最末尾加上

sql_mode=STRICT_TRANS_TABLES,NO_ZERO_IN_DATE,NO_ZERO_DATE,ERROR_FOR_DIVISION_BY_ZERO,NO_AUTO_CREATE_USER,NO_ENGINE_SUBSTITUTION

重啟MySql服務,MySQLSyntaxErrorException異常的問題就解決

systemctl restart mysqld

[[email protected] etc]# systemctl status mysqld
● mysqld.service - MySQL Server
   Loaded: loaded (/usr/lib/systemd/system/mysqld.service; enabled; vendor preset: disabled)
   Active: active (running) since Thu 2018-10-04 10:38:53 CST; 1 months 22 days ago
     Docs: man:mysqld(8)
           http://dev.mysql.com/doc/refman/en/using-systemd.html
 Main PID: 10742 (mysqld)
   CGroup: /system.slice/mysqld.service
           └─10742 /usr/sbin/mysqld --daemonize --pid-file=/var/run/mysqld/mysqld.pid

Oct 04 10:38:51 majker systemd[1]: Starting MySQL Server...
Oct 04 10:38:53 majker systemd[1]: Started MySQL Server.
[[email protected] etc]# 
[[email protected] etc]# 
[[email protected] etc]# 
[[email protected] etc]# systemctl restart  mysqld
[[email protected] etc]# ps -ef |grep mysql
mysql    13782     1  4 21:25 ?        00:00:00 /usr/sbin/mysqld --daemonize --pid-file=/var/run/mysqld/mysqld.pid
root     13814 13650  0 21:26 pts/0    00:00:00 grep --color=auto mysql
[ro[email protected] etc]# mysql -uroot -p

效果

在這裡插入圖片描述