Expression #1 of ORDER BY clause is not in SELECT list,references column 'xxxxxxx'

給數據庫升了個級,後臺紛紛報錯誤:

報錯信息: 

Expression #1 of ORDER BY clause is not in SELECT list, references column 'xxxx' which is not in SELECT list; this is incompatible with DISTINCT

問題原因:

  mysql5.7.5及以上版本將sql_mode的ONLY_FULL_GROUP_BY模式默認設置爲打開狀態,會導致一些錯誤:

1、我們使用GROUP BY查詢時,出現在SELECT字段後面的只能是GROUP BY後面的分組字段,或使用聚合函數包裹着的字段,否則會報錯如下信息:
  Expression #1 of SELECT list is not in GROUP BY clause and contains nonaggregated column 'database.table.column' which is not functionally dependent on columns in GROUP BY clause; this is incompatible with sql_mode=only_full_group_by
2、當使用ORDER BY查詢時,不能使用SELECT DISTINCT去重查詢。否則會報錯如下信息:
   Expression #1 of ORDER BY clause is not in SELECT list, references column 'database.table.column' which is not in SELECT list; this is incompatible with DISTINCT

查詢驗證:

select version(); 查詢版本

select @@global.sql_mode   查詢sql_mode

 

解決方法:

   去除ONLY_FULL_GROUP_BY

1、通過命令關閉:

set global 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服務後會失效,重啓服務後會失效


2、通過修改mysql的配置文件關閉ONLY_FULL_GROUP_BY SQL模式

sudo vim /etc/mysql/conf.d/mysql.cnf

文件底部追加:

[mysqld]
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

sudo service mysql restart

 

轉自:https://www.cnblogs.com/start-fxw/p/11882230.html

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章