我用WAMP服务器在我的windows PC上使用MySQL 5.7.13

我的问题是在执行这个查询时

SELECT *
FROM `tbl_customer_pod_uploads`
WHERE `load_id` = '78' AND
      `status` = 'Active'
GROUP BY `proof_type`

我总是得到这样的错误

SELECT列表中的表达式#1不在GROUP BY子句中,包含未聚合的列returntr_prod.tbl_customer_pod_uploads。id',它不依赖于GROUP BY子句中的列;这与sql_mode=only_full_group_by不兼容

你能告诉我最好的解决办法吗?

我需要这样的结果

+----+---------+---------+---------+----------+-----------+------------+---------------+--------------+------------+--------+---------------------+---------------------+
| id | user_id | load_id | bill_id | latitude | langitude | proof_type | document_type | file_name    | is_private | status | createdon           | updatedon           |
+----+---------+---------+---------+----------+-----------+------------+---------------+--------------+------------+--------+---------------------+---------------------+
|  1 |       1 | 78      | 1       | 21.1212  | 21.5454   |          1 |             1 | id_Card.docx |          0 | Active | 2017-01-27 11:30:11 | 2017-01-27 11:30:14 |
+----+---------+---------+---------+----------+-----------+------------+---------------+--------------+------------+--------+---------------------+---------------------+

当前回答

For the query to be legal in SQL92, the name column must be omitted from the select list or named in the GROUP BY clause. SQL99 and later permits such nonaggregates per optional feature T301 if they are functionally dependent on GROUP BY columns: If such a relationship exists between name and custid, the query is legal. This would be the case, for example, were custid a primary key of customers. MySQL 5.7.5 and up implements detection of functional dependence. If the ONLY_FULL_GROUP_BY SQL mode is enabled (which it is by default), MySQL rejects queries for which the select list, HAVING condition, or ORDER BY list refer to nonaggregated columns that are neither named in the GROUP BY clause nor are functionally dependent on them. via MySQL :: MySQL 5.7 Reference Manual :: 12.19.3 MySQL Handling of GROUP BY

你可以用下面的命令改变sql模式来解决这个问题:

SET GLOBAL sql_mode=(SELECT (@@sql_mode,'ONLY_FULL_GROUP_BY', "));

和…记得重新连接数据库!!

其他回答

当MySQL的only_full_group_by模式被打开时,这意味着严格的ANSI SQL规则将在使用GROUP BY时应用。对于你的查询,这意味着如果你对proof_type列进行GROUP BY,那么你只能选择两件事:

proof_type列,或者 任何其他列的聚合

通过其他列的“聚合”,我指的是对另一列使用诸如MIN()、MAX()或AVG()之类的聚合函数。所以在你的情况下,下面的查询是有效的:

SELECT proof_type,
       MAX(id) AS max_id,
       MAX(some_col),
       MIN(some_other_col)
FROM tbl_customer_pod_uploads
WHERE load_id = '78' AND
      status = 'Active'
GROUP BY proof_type

我在SO上看到的绝大多数MySQL GROUP BY问题都关闭了严格模式,所以查询正在运行,但结果不正确。在您的情况下,查询根本不会运行,迫使您考虑真正想要做什么。

注意:ANSI SQL扩展了GROUP BY中允许选择的内容,也包括了功能上依赖于所选列的列。函数依赖的一个例子是按表中的主键列分组。由于保证主键对于每条记录都是唯一的,因此任何其他列的值也将确定。MySQL是允许这样做的数据库之一(SQL Server和Oracle不AFAIK)。

在Ubuntu中

步骤1:

sudo vi /etc/mysql/mysql.conf.d/mysqld.cnf

第二步:转到最后一行并添加以下内容

sql_mode = ""

第三步:保存

步骤4:重启mysql服务器。

This

SELECT列表中的表达式#1不在GROUP BY子句中,包含未聚合的列returntr_prod.tbl_customer_pod_uploads。id',它不依赖于GROUP BY子句中的列;这与sql_mode=only_full_group_by不兼容

将通过这个命令改变MySQL中的sql模式来简单地解决,

SET GLOBAL sql_mode=(SELECT REPLACE(@@sql_mode,'ONLY_FULL_GROUP_BY',''));

这也适用于我。 我使用这个,因为在我的项目中有很多这样的查询,所以我只是将这个sql模式更改为only_full_group_by

或者简单地包括SELECT语句指定的GROUP BY子句中的所有列。sql_mode可以保持启用状态。

谢谢你!: -)

在运行查询之前运行下面的查询

SET SQL_MODE = '';

适用于CentOS/RHEL (Linux服务器)和XAMPP本地操作系统

你好,我有一个非常不同的解决方案,我希望这能帮助到一些人。(可能看起来有点过分,但这对我来说真的很有用,不像其他解决方案)

我正在运行CentOS 7,我所有的代码都在本地工作,但当我上传到我的服务器时,我开始得到这个问题解决的错误。

几个小时后,我尝试从不同的角度来看这个问题,我记得我的本地设置使用XAMPP,我以为我使用的是MySQL 8 (MySQL 8安装在我的服务器上)。

但是从XAMPP登录到MySql后,我得到了下面的输出:

mysql -u root -p //Login


Welcome to the MariaDB monitor.  Commands end with ; or \g.
Your MariaDB connection id is 27
Server version: 10.4.18-MariaDB mariadb.org binary distribution

Copyright (c) 2000, 2018, Oracle, MariaDB Corporation Ab and others.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

是的,你可能已经注意到同样的,XAMPP使用MariaDB,所以解决方案是卸载mysql并安装MariaDB

当我运行Centos 7时,这样做的步骤如下:

从你的服务器删除mysql


# yum remove mysql mysql-server

更新你的系统(这是常见的每次我们安装新东西)


# sudo yum  -y update

将MariaDB添加到存储库


# sudo tee /etc/yum.repos.d/MariaDB.repo<<EOF 
[mariadb]
name = MariaDB
baseurl = http://yum.mariadb.org/10.4/centos7-amd64
gpgkey=https://yum.mariadb.org/RPM-GPG-KEY-MariaDB
gpgcheck=1
EOF

更新缓存


# sudo yum makecache fast

安装 MariaDB


# sudo yum -y install MariaDB-server MariaDB-client

添加MariaDB到启动(每次系统重新启动时启动MariaDB)


#sudo systemctl enable --now mariadb

然后你可以通过运行下面的命令来保护你的安装:

# sudo mysql_secure_installation

最后一个命令将开始一个过程,您可以在其中设置密码和其他选项。

之后不要忘记添加非root用户,并授予它必要的特权,这是因为你不能在你的应用程序中使用root用户(是的,我必须弄清楚这一点)

首次使用root帐号登录:

# mysql -u root -p

然后添加您的用户并授予权限:

CREATE USER 'YourUserName'@localhost IDENTIFIED BY 'YourPassword';
GRANT ALL PRIVILEGES ON *.* TO 'YourUserName'@localhost IDENTIFIED BY 'YourPassword' WITH GRANT OPTION;

最后,你必须创建你的数据库,导入你的表/数据/触发器/过程。

现在您将能够毫无问题地运行代码,就像您的本地设置一样。(也许你将不得不安装mysql扩展,以防你不得不在本地做)。