解决MySql查询语句报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

效果

在这里插入图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值