解决MySql查询语句报MySQLSyntaxErrorException异常的问题

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/Majker/article/details/84503081

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

[root@majker 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.
[root@majker etc]# 
[root@majker etc]# 
[root@majker etc]# 
[root@majker etc]# systemctl restart  mysqld
[root@majker 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
[root@majker etc]# mysql -uroot -p

效果

在这里插入图片描述

猜你喜欢

转载自blog.csdn.net/Majker/article/details/84503081
今日推荐