我用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 |
+----+---------+---------+---------+----------+-----------+------------+---------------+--------------+------------+--------+---------------------+---------------------+

当前回答

如果你正在使用PhpMyAdmin,搜索“SQL模式”,然后去掉值:ONLY_FULL_GROUP_BY, just did and it ok。

其他回答

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可以保持启用状态。

谢谢你!: -)

在MySql引擎中有一个系统变量ONLY_FULL_GROUP_BY。

从Mysql版本5.7.5:只有_full_group_by SQL模式默认启用

5.7.5版本之前:默认不启用ONLY_FULL_GROUP_BY。

如果启用了ONLY_FULL_GROUP_BY SQL模式(这是5.7.5版本的默认设置),MySQL将拒绝select列表、HAVING条件或ORDER by列表引用非聚合列的查询,这些列既不在GROUP by子句中命名,也在功能上依赖于它们。

为了解决问题,可以使用以下3种解决方案中的任意一种

(1) PHPMyAdmin

不启用:只有full_group_by模式

如果您正在使用phpMyAdmin,则更改sql_mode设置,如下面的截图所示。

编辑sql模式变量并从值中删除ONLY_FULL_GROUP_BY文本

OR

(2) SQL/命令提示

禁用:只使用“full_group_by”模式。

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

OR

不要使用SELECT *

不要禁用ONLY_FULL_GROUP_BY模式,但是

在SELECT查询中使用相关列。相关的是指列,它们要么是按子句分组,要么是带有聚合函数的列(MAX, MIN, SUM, COUNT等)


重要提示

使用点(1)或点(2)所做的更改不会将其设置为永久,并且在每次重新启动后将恢复。

所以你应该在你的配置文件中设置这个(例如/etc/mysql/my.cnf在[mysqld]部分),这样更改在MySQL重启后仍然有效:

配置文件:/etc/mysql/my.cnf

变量名:sql_mode或sql-mode

从值中删除单词ONLY_FULL_GROUP_BY并保存文件。

注意:如果在配置文件中没有找到sql_mode变量,请在文件末尾插入以下2行

[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

登录到phpMyAdmin 导航到:Server: localhost:3306并执行 不选择任何数据库 点击顶部菜单中的变量 搜索“sql mode”并将相应的值编辑为:NO_ENGINE_SUBSTITUTION

这是所有。

我在我的Ec2中这样做了,它很有魅力。

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', "));

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

两种解决方案:

)使用

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

b)可以在不禁用ONLY_FULL_GROUP_BY的情况下使用ANY_VALUE()引用非聚合列来达到同样的效果。

ANY_VALUE(值)