mysql+5.7+error+2000_mysql从5.6升级到5.7后出现 Expression #1 of ORDER BY clause is not i

【问题】

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

【场景】

老库

[email protected]:#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)

新库

[[email protected] ~]# 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 i"希望对你有所帮助.本文来自网络,转载请注明出处:http://www.weixuecn.cn/article/9357.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值