【问题】
mysql从5.6升级到5.7后出现:插入数据和修改数据时出错
Caused by: com.ibatis.common.jdbc.exception.NestedSQLException:
--- The error occurred while applying a parameter map.
--- Check the findOrderList-InlineParameterMap.
--- Check the statement (query failed).
--- Cause: java.sql.SQLException: Expression #1 of ORDER BY clause is not in SELECT list, references column 'ddfei.t2.add_time' which is not in SELECT list; this is incompatible with DISTINCT
at com.ibatis.sqlmap.engine.mapping.statement.MappedStatement.executeQueryWithCallback(MappedStatement.java:201)
at com.ibatis.sqlmap.engine.mapping.statement.MappedStatement.executeQueryForList(MappedStatement.java:139)
at com.ibatis.sqlmap.engine.impl.SqlMapExecutorDelegate.queryForList(SqlMapExecutorDelegate.java:567)
at com.ibatis.sqlmap.engine.impl.SqlMapExecutorDelegate.queryForList(SqlMapExecutorDelegate.java:541)
at com.ibatis.sqlmap.engine.impl.SqlMapSessionImpl.queryForList(SqlMapSessionImpl.java:118)
at org.springframework.orm.ibatis.SqlMapClientTemplate$3.doInSqlMapClient(SqlMapClientTemplate.java:295)
at org.springframework.orm.ibatis.SqlMapClientTemplate$3.doInSqlMapClient(SqlMapClientTemplate.java:1)
at org.springframework.orm.ibatis.SqlMapClientTemplate.execute(SqlMapClientTemplate.java:200)
... 43 more
Caused by: java.sql.SQLException: Expression #1 of ORDER BY clause is not in SELECT list, references column 'ddfei.t2.add_time' which is not in SELECT list; this is incompatible with DISTINCT
at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:964)
at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:3970)
at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:3906)
at com.mysql.jdbc.MysqlIO.sendCommand(MysqlIO.java:2524)
at com.mysql.jdbc.MysqlIO.sqlQueryDirect(MysqlIO.java:2677)
at com.mysql.jdbc.ConnectionImpl.execSQL(ConnectionImpl.java:2549)
at com.mysql.jdbc.PreparedStatement.executeInternal(PreparedStatement.java:1861)
at com.mysql.jdbc.PreparedStatement.execute(PreparedStatement.java:1192)
at com.alibaba.druid.filter.FilterChainImpl.preparedStatement_execute(FilterChainImpl.java:2931)
at com.alibaba.druid.wall.WallFilter.preparedStatement_execute(WallFilter.java:588)
at com.alibaba.druid.filter.FilterChainImpl.preparedStatement_execute(FilterChainImpl.java:2929)
at com.alibaba.druid.filter.FilterEventAdapter.preparedStatement_execute(FilterEventAdapter.java:440)
at com.alibaba.druid.filter.FilterChainImpl.preparedStatement_execute(FilterChainImpl.java:2929)
at com.alibaba.druid.filter.FilterEventAdapter.preparedStatement_execute(FilterEventAdapter.java:440)
at com.alibaba.druid.filter.FilterChainImpl.preparedStatement_execute(FilterChainImpl.java:2929)
at com.alibaba.druid.proxy.jdbc.PreparedStatementProxyImpl.execute(PreparedStatementProxyImpl.java:118)
at com.alibaba.druid.pool.DruidPooledPreparedStatement.execute(DruidPooledPreparedStatement.java:493)
at com.ibatis.sqlmap.engine.execution.SqlExecutor.executeQuery(SqlExecutor.java:185)
at com.nbtv.orm.dao.ibatis.executor.LimitSqlExecutor.executeQuery(LimitSqlExecutor.java:57)
at com.ibatis.sqlmap.engine.mapping.statement.MappedStatement.sqlExecuteQuery(MappedStatement.java:221)
at com.ibatis.sqlmap.engine.mapping.statement.MappedStatement.executeQueryWithCallback(MappedStatement.java:189)
... 50 more

【场景】
老库
root@<ddfei-mysq01|~>:#mysql -uroot -p
Enter password:
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 5905
Server version: 5.6.40-log MySQL Community Server (GPL)

mysql> show variables like '%sql_mode%';
+---------------+--------------------------------------------+
| Variable_name | Value |
+---------------+--------------------------------------------+
| sql_mode | STRICT_TRANS_TABLES,NO_ENGINE_SUBSTITUTION |
+---------------+--------------------------------------------+
1 row in set (0.00 sec)

新库
[root@ddfei-mysql01 ~]# mysql -uroot -p
Enter password:
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 39
Server version: 5.7.28 Source distribution

Copyright (c) 2000, 2019, Oracle and/or its affiliates. All rights reserved.

Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.

【解决】
mysql> select @@global.sql_mode;
+-------------------------------------------------------------------------------------------------------------------------------------------+
| @@global.sql_mode |
+-------------------------------------------------------------------------------------------------------------------------------------------+
| ONLY_FULL_GROUP_BY,STRICT_TRANS_TABLES,NO_ZERO_IN_DATE,NO_ZERO_DATE,ERROR_FOR_DIVISION_BY_ZERO,NO_AUTO_CREATE_USER,NO_ENGINE_SUBSTITUTION |
+-------------------------------------------------------------------------------------------------------------------------------------------+
1 row in set (0.01 sec)

mysql> set @@global.sql_mode='';
Query OK, 0 rows affected, 1 warning (0.00 sec)

mysql> set @@global.sql_mode = 'STRICT_TRANS_TABLES,NO_ENGINE_SUBSTITUTION';
Query OK, 0 rows affected, 2 warnings (0.00 sec)

mysql> SELECT @@GLOBAL.sql_mode;
+--------------------------------------------+
| @@GLOBAL.sql_mode |
+--------------------------------------------+
| STRICT_TRANS_TABLES,NO_ENGINE_SUBSTITUTION |
+--------------------------------------------+
1 row in set (0.00 sec)

这样只是暂时修改,永久生效可以改配置文件my.cnf 然后 service mysqld restart 生效

【原因】
可能是
1、在sql查询语句中不需要group by的字段上使用any_value()函数
这种对于已经开发了不少功能的项目不太合适,毕竟要把原来的sql都给修改一遍

2、DISTINCT和order by都会对数据进行排序操作,所以会产生冲突
在sql语句中使用DISTINCT时不使用order by进行排序,获取结果集后通过php进行数据的排序,同时也提高了mysql的性能。同时group by,limit和其中的一起搭配使用也会导致错误。
mysql5.7版本中,如果DISTINCT和order by一起使用将会报3065错误,sql语句无法执行。这是由于5.7版本语法比之前版本语法要求更加严格导致的。

3、
MySQL Server 默认开启了 sql_mode=only_full_group_by 模式,此模式要求 group by 字段必须出现在查询项中(select),否则就会报出该错误。因为GROUP BY处理变得更加复杂,包括检测功能依赖性。

【补充】
查询sql_mode的方式
查询全局sql_mode
SELECT @@GLOBAL.sql_mode;
查询当前会话sql_mode
SELECT @@SESSION.sql_mode;
...
【参考】
https://www.cnblogs.com/liukaifeng/p/10103810.html

官方翻译说明
mysql5.6升级到5.7后 linux下修改mysql的sql_mode模式
https://blog.csdn.net/xu1988923/article/details/89310458
转自:高效码农:https://www.xugj520.cn/archives/68.html

mysql从5.6升级到5.7后出现 Expression #1 of ORDER BY clause is not in SELECT list,this is incompatible with DISTINCT的更多相关文章

  1. CentOS6.5把MySQL从5.1升级到5.6后,MySQL不能启动

    解决了:进入mysql安装目录 cd /var/lib/mysql删除了如下三个文件:ibdata1  ib_logfile0  ib_logfile1 CentOS6.5把MySQL从5.1升级到5 ...

  2. mysql错误:[Err] 1055 - Expression #1 of ORDER BY clause is not in GROUP BY clause and contains nonaggregated

    今天迁移django数据库的时候,跑程序的时候出现这样的错误: [Err] 1055 - Expression #1 of ORDER BY clause is not in GROUP BY cla ...

  3. mysql(5.7以上)查询报错:ORDER BY clause is not in GROUP BY..this is incompatible with sql_mode=only_full_group_by

    执行mysql命令查询时: select * from table_name错误信息如: [Err] 1055 - Expression #1 of ORDER BY clause is not in ...

  4. MySQL [Err] 1055 - Expression #1 of ORDER BY clause is not in GROUP BY clause

    MySQL[Err]1055 上次MySQL5.7.19主从建立完成之后,所有的测试都是在MySQL命令行下进行的,最近用Navicat Premium进行MySQL的连接,然后在插入数据的时候MyS ...

  5. 解决mysql报错:- Expression #1 of ORDER BY clause is not in GROUP BY clause and contains nonaggregated column 'information_schema.PROFILING.SEQ'

    mysql执行报错: - Expression #1 of ORDER BY clause is not in GROUP BY clause and contains nonaggregated c ...

  6. mysql [Err] 1055 - Expression #1 of ORDER BY clause is not in GROUP BY clause and contains nonaggregated column 'information_schema.PROFILING.SEQ' which is not functionally dependent on columns in GRO

    [Err] 1055 - Expression #1 of ORDER BY clause is not in GROUP BY clause and contains nonaggregated c ...

  7. mysql Expression #1 of ORDER BY clause is not in GROUP BY clause and contains nona

    1. 操作mysql的时候提示如下错误 [Err] 1055 - Expression #1 of ORDER BY clause is not in GROUP BY clause and cont ...

  8. mysql 5.7 Expression #1 of ORDER BY clause is not in GROUP BY clause and contains nonaggregated column ...报错

    使用mysql在执行一条插入语句时 , ', "hhh"); 报错:Expression #1 of ORDER BY clause is not in GROUP BY clau ...

  9. windows server 2008 安装MySQL 8.0 遇到报错 1055 - Expression #1 of ORDER BY clause is not in GROUP BY

    mysql安装参考教程:https://blog.csdn.net/qq_37350706/article/details/81707862 安装完毕后 执行sql语句 SELECT * FROM c ...

随机推荐

  1. Android多图选择

    多图选择是Android中一个常用的功能,用户可以拍照或者批量选择图片上传,还是国际惯例,先看下效果图,demo地址我会放到文章末尾. 经过对比,这里我选择了一个第三方开源库PictureSelect ...

  2. React、React Native面试题

    1.React Native相对于原生的ios和Android有哪些优势. react native一套代码可以开发出跨平台app, 减少了人力.节省了时间.避免了 iOS 与 Android 版本发 ...

  3. Java修炼——四种方式解析XML_SAX

    四种方式解析XML:DOM      JDOM    DOM4J    SAX 先写一个XML栗子: <?xml version="1.0" encoding="U ...

  4. ThreadLocal的进化——InheritableThreadLocal

    之前有介绍过 ThreadLocal,JDK 后来针对此做了一个升级版本 InheritableThreadLocal,今天就来好好介绍下. 为什么要升级 首先我们来想想,为什么要升级?这就要说起 T ...

  5. 为什么有ASP.NET

    最近读了一些文章,总结一下: 在1999年,当时微软的windows系统运行的所有的应用程序都是有组件对象模型为根本基础开发的,用VB来处理数据访问和复杂的用户界面,缺点是不能使用函数指针,因为当时的 ...

  6. Electron 设置 -webkit-app-region 后无法响应鼠标点击事件的解决方式

    参考博客:https://blog.csdn.net/qq_20264891/article/details/87721163

  7. 【朝花夕拾】Android多线程之(三)runOnUiThread篇——程序猿们的贴心小棉袄

    runOnUiThread()的使用以及原理实在是太简单了,简单到笔者开始都懒得单独开一篇文章来写它.当然这里说的简单,是针对对Handler比较熟悉的童鞋而言的.不过麻雀虽小,五脏俱全,runOnU ...

  8. Windows下mysql-5.7.28下载、安装、配置教程

    最近需要更换mysql数据库的版本,写一篇文章,记录一下 一.下载mysql数据库 mysql的下载共有两种,一种是zip压缩文件,另一种是msi安装程序 官方5.7版本zip压缩文件下载页面 官方5 ...

  9. monkey命令解析详解

      我面试时遇到过几次让背个monkey命令的,可以这样简单说一个:adb shell monkey -p(约束包名) -s 200 -v -v --throttle 300 1500000 > ...

  10. 2019年Java面试题基础系列228道(3)

    51.类 ExampleA 继承 Exception,类 ExampleB 继承ExampleA. 有如下代码片断: try { throw new ExampleB("b")}c ...