个性化阅读
专注于IT技术分析

MySQL语法错误或访问冲突:1055 Expression of SELECT list is not in GROUP BY clause and contains nonaggregated column which is not functionally dependent on columns in GROUP BY clause

点击下载

本文概述

几周前, 在一个需要升级并移至新服务器的旧项目中工作, 使我陷入了该应用程序几个模块的下一个例外:

SELECT列表的表达式不在GROUP BY子句中, 并且包含未聚合的列, 这与sql_mode = only_full_group_by不兼容。

根据MySQL文档, 在sql模式下只有唯一的完全group by模式将拒绝选择列表, HAVING条件或ORDER BY列表引用未在GROUP BY子句中命名且在功能上不相关的非聚合列的查询在(唯一确定)GROUP BY列上。

在某些情况下, 由于标准查询无法在mysql客户端中使用默认配置, 因此, 你可能会希望以一种不再出现异常的方式重写它, 但这是由于时间和开销所致开发中, 你将需要一个更快而又不那么昂贵的解决方案。该解决方案的要点是, 该查询在具有不同设置的旧版MySQL中工作, 因此你可以通过简单地更改MySQL客户端的sql模式来运行相同的查询, 我们将在短时间内向你展示如何执行此查询。文章。

1.找到MySQL my.cnf文件

你需要做的第一件事是找到MySQL的配置文件。例如, 在具有许多发行版的Plesk的情况下, 你可以在/etc/mysql/my.cnf或/etc/my.cfn中找到该文件, 但这可能有所不同, 因此你可以使用linux命令找到该文件, 例如:

find / -name my.cnf

这将输出具有该名称的文件的路径。找到文件后, 你将能够更改设置, 以消除出现的异常。

2.修改sql模式

在[mysqld]设置块内, 你需要将sql_mode属性的值更新为空字符串, 这将删除’only_full_group_by’模式:

# Inside the mysqld block
[mysqld]
# add new option
sql_mode = ""

使用nano, vi, emacs或通过SFTP编辑my.cnf文件并保存更改。例如, my.cnf文件最终将具有以下新设置:

#
# The MySQL database server configuration file.
#
# You can copy this to one of:
# - "/etc/mysql/my.cnf" to set global options, # - "~/.my.cnf" to set user-specific options.
#
# One can use all long options that the program supports.
# Run program with --help to get a list of available options and with
# --print-defaults to see which it would actually understand and use.
#
# For explanations see
# http://dev.mysql.com/doc/mysql/en/server-system-variables.html

#
# * IMPORTANT: Additional settings that can override those from this file!
#   The files must end with '.cnf', otherwise they'll be ignored.
#

!includedir /etc/mysql/conf.d/
!includedir /etc/mysql/mysql.conf.d/
[mysqld]
sql_mode=ONLY_FULL_GROUP_BY, NO_ZERO_IN_DATE, ERROR_FOR_DIVISION_BY_ZERO, NO_AUTO_CREATE_USER, NO_ENGINE_SUBSTITUTION
bind-address = ::ffff:127.0.0.1
local-infile=0

# Important: remove limitation of group by with the following line
sql_mode = ""

将更改保存在文件中, 并根据你的操作系统和安装过程使用cli重新启动mysql, 例如:

# Ubuntu
sudo service mysql restart

# CentOS
/etc/init.d/mysqld start

现在, 具有该问题的查询现在应该可以运行, 并且不再出现异常。

编码愉快!

赞(1)
未经允许不得转载:srcmini » MySQL语法错误或访问冲突:1055 Expression of SELECT list is not in GROUP BY clause and contains nonaggregated column which is not functionally dependent on columns in GROUP BY clause

评论 抢沙发

评论前必须登录!