我用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', "));
和…记得重新连接数据库!!
only_full_group_by = on告诉MySQL引擎:当你怀疑要显示什么结果并抛出错误时,不要应用GROUP BY。只有当Group By明确告诉您要做什么时才应用它。即当Group By已满且完全时!
only_full_group_by = off告诉MySQL引擎:总是应用GROUP BY,如果你对选择什么结果有疑问,就随机选择一个!
如果你正确使用GROUP BY,你不必关闭它!
例子:
表:用户
id | name
----------------
1 ali
2 john
3 ali
在name列上使用GROUP BY时:
SELECT * FROM users GROUP BY name;
有两种可能的结果:
1 ali
2 john
OR
2 john
3 ali
MYSQL不知道选择什么结果!因为有不同的id,但都有name=ali。
Solution1:
只选择名称字段:
SELECT name FROM users GROUP BY name;
结果:
ali
john
这是一个完美的解决方案。删除使GROUP BY混淆的列。这意味着你知道你在做什么。通常,你不需要
这些列,但如果您需要它们,请转到解决方案3!
Solution2:
关闭only_full_group_by。MYSQL将从两个可能的结果中随机选择一个!!(如果您不关心它将选择什么id,这是可以的,但请记住在查询后立即打开它,以防止将来的groupby中出现意外行为)
环境
使用像MIN(), MAX()这样的聚合函数来帮助MYSQL决定它必须选择什么。
例如:
SELECT MAX(id), name FROM users GROUP BY name;
它将选择具有最大id的ali行:
2 john
3 ali
如果你在Mac上使用MAMP,当你重新启动MAMP时,对MySQL变量的任何更改都将被擦除。要确保更改是永久性的,请执行以下操作:
停止正在运行的MAMP服务器。
使用文本编辑器创建一个名为my.cnf的文件,并将其保存到/Applications/MAMP/conf文件夹中。将文件放在文件夹的根目录下(这看起来有点奇怪,因为它包含了大量的apache文件夹,但这是可以的)。在文件中添加如下代码行:
[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
保存文件并关闭编辑器。
启动MAMP服务器。
这将使改变成为永久性的。
在我的例子中,这是我的旧sql_mode
sql_mode="ONLY_FULL_GROUP_BY,STRICT_TRANS_TABLES,NO_ZERO_IN_DATE,NO_ZERO_DATE,ERROR_FOR_DIVISION_BY_ZERO,NO_ENGINE_SUBSTITUTION"
您可以使用
show variables like "sql_mode";
然后我把它设置成这样
SET sql_mode='STRICT_TRANS_TABLES,NO_ZERO_IN_DATE,NO_ZERO_DATE,ERROR_FOR_DIVISION_BY_ZERO,NO_ENGINE_SUBSTITUTION';