mysql從5.6升級到5.7後出現 Expression #1 of ORDER BY clause is not in SELECT list,this is incompatible with DISTINCT

来源:https://www.cnblogs.com/ritchy/archive/2019/10/29/11757948.html
-Advertisement-
Play Games

【問題】mysql從5.6升級到5.7後出現:插入數據和修改數據時出錯Caused by: com.ibatis.common.jdbc.exception.NestedSQLException: The error occurred while applying a parameter map. ...


【問題】
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


您的分享是我們最大的動力!

-Advertisement-
Play Games
更多相關文章
  • 1、描述zookeeper集群中leader,follower,observer幾種角色 Zookeeper: 分散式系統:是一個硬體或軟體組件分佈在網路中的不同的電腦之上,彼此間僅通過消息傳遞進行通信和協作的系統。 特征: 分佈性、對等性、併發性、缺乏全局時鐘、故障必然會發生 典型問題: 通信異 ...
  • 部署 istio 1.添加 istio 官方的 helm 倉庫 2.是否添加成功 NAME CHART VERSION APP VERSION DESCRIPTION istio/istio 1.3.3 1.3.3 Helm chart for all istio components istio/ ...
  • 安裝環境準備工具yum –y install oracle-rdbms-server-11gR2-preinstall創建目錄mkdir -p /u01/app/oracle/product/11.2.0/dbhome_1mkdir -p /u02/oradatachown -R oracle:oi... ...
  • Linux下shell通用腳本啟動jar(微服務) vim app_jar.sh 使用方式: app_jar.sh start app_jar.sh stop app_jar.sh restart 註:該腳本只需要放在jar包當前目錄下即可,腳本當前目錄只允許存在一個jar包,如果多個會同時啟動或重 ...
  • 解決因編譯php中添加了-enable-gd-jis-conv選項導致Zabbix監控系統圖形界面中文亂碼問題 現象: php編譯參數: 說明: 如果PHP編譯時啟用–enable-gd-jis-conv選項的話,那麼非ASCII字元(例如漢字、拼音、希臘文和箭頭) 會被當成EUC-JP編碼 (ph ...
  • [TOC] 創建數據表 列約束 auto_increment 自增長1 primary key 主鍵索引,加快查詢速度,列的值不能重覆 not null 標識該欄位不能為空 default 該欄位設置預設值 查看數據表結構 列類型(欄位類型) 整型 | 類型 | 大小 | 範圍(無符號) | | | ...
  • 作業 ...
  • [toc] 1. 資料庫是什麼 存數據的倉庫 2. 為什麼使用資料庫 1. 管理大量數據 2. 支持併發操作 3. 支持高級的操作,比如分組,鏈表等 3. 資料庫的分類 1. 關係型資料庫 表結構存儲,對每一列的數據的類型會有約束,數據存在硬碟中 Mysql,maridb(免費,企業用的多),Sql ...
一周排行
    -Advertisement-
    Play Games
  • 移動開發(一):使用.NET MAUI開發第一個安卓APP 對於工作多年的C#程式員來說,近來想嘗試開發一款安卓APP,考慮了很久最終選擇使用.NET MAUI這個微軟官方的框架來嘗試體驗開發安卓APP,畢竟是使用Visual Studio開發工具,使用起來也比較的順手,結合微軟官方的教程進行了安卓 ...
  • 前言 QuestPDF 是一個開源 .NET 庫,用於生成 PDF 文檔。使用了C# Fluent API方式可簡化開發、減少錯誤並提高工作效率。利用它可以輕鬆生成 PDF 報告、發票、導出文件等。 項目介紹 QuestPDF 是一個革命性的開源 .NET 庫,它徹底改變了我們生成 PDF 文檔的方 ...
  • 項目地址 項目後端地址: https://github.com/ZyPLJ/ZYTteeHole 項目前端頁面地址: ZyPLJ/TreeHoleVue (github.com) https://github.com/ZyPLJ/TreeHoleVue 目前項目測試訪問地址: http://tree ...
  • 話不多說,直接開乾 一.下載 1.官方鏈接下載: https://www.microsoft.com/zh-cn/sql-server/sql-server-downloads 2.在下載目錄中找到下麵這個小的安裝包 SQL2022-SSEI-Dev.exe,運行開始下載SQL server; 二. ...
  • 前言 隨著物聯網(IoT)技術的迅猛發展,MQTT(消息隊列遙測傳輸)協議憑藉其輕量級和高效性,已成為眾多物聯網應用的首選通信標準。 MQTTnet 作為一個高性能的 .NET 開源庫,為 .NET 平臺上的 MQTT 客戶端與伺服器開發提供了強大的支持。 本文將全面介紹 MQTTnet 的核心功能 ...
  • Serilog支持多種接收器用於日誌存儲,增強器用於添加屬性,LogContext管理動態屬性,支持多種輸出格式包括純文本、JSON及ExpressionTemplate。還提供了自定義格式化選項,適用於不同需求。 ...
  • 目錄簡介獲取 HTML 文檔解析 HTML 文檔測試參考文章 簡介 動態內容網站使用 JavaScript 腳本動態檢索和渲染數據,爬取信息時需要模擬瀏覽器行為,否則獲取到的源碼基本是空的。 本文使用的爬取步驟如下: 使用 Selenium 獲取渲染後的 HTML 文檔 使用 HtmlAgility ...
  • 1.前言 什麼是熱更新 游戲或者軟體更新時,無需重新下載客戶端進行安裝,而是在應用程式啟動的情況下,在內部進行資源或者代碼更新 Unity目前常用熱更新解決方案 HybridCLR,Xlua,ILRuntime等 Unity目前常用資源管理解決方案 AssetBundles,Addressable, ...
  • 本文章主要是在C# ASP.NET Core Web API框架實現向手機發送驗證碼簡訊功能。這裡我選擇是一個互億無線簡訊驗證碼平臺,其實像阿裡雲,騰訊雲上面也可以。 首先我們先去 互億無線 https://www.ihuyi.com/api/sms.html 去註冊一個賬號 註冊完成賬號後,它會送 ...
  • 通過以下方式可以高效,並保證數據同步的可靠性 1.API設計 使用RESTful設計,確保API端點明確,並使用適當的HTTP方法(如POST用於創建,PUT用於更新)。 設計清晰的請求和響應模型,以確保客戶端能夠理解預期格式。 2.數據驗證 在伺服器端進行嚴格的數據驗證,確保接收到的數據符合預期格 ...