gpt4 book ai didi

mysql - ONLY_FULL_GROUP_BY 未设置但仍然有错误 1140

转载 作者:行者123 更新时间:2023-11-29 00:32:18 25 4
gpt4 key购买 nike

我正在完成我的网站的实现,但现在我在网上遇到了一个我在本地没有遇到的问题。

我收到这个错误:

failed: Mixing of GROUP columns (MIN (), MAX (), COUNT (), ...) with no GROUP columns is illegal if there is no GROUP BY clause

SQL 查询的结果

我在网上搜索了很多论坛,大多数用户建议更改我不能/不想的查询,或者他们说它可能处于 sql-mode: enabled ONLY_FULL_GROUP_BY服务器

我的在线服务器上的 sql-mode 是空的(我可以通过查询 select @@sql_mode; 看到)更确切地说,我将 sql_mode='' 放在 my.cnf 中

但问题依旧。

这是由于我的在线服务器上的 mysql 5.0.44 版本和本地 5.1.32(我没有这个错误......)造成的吗?

最佳答案

是的。你是对的。发生这种情况是因为 MySQL 版本。
查看我的回答here

如何查看MySQL版本?

mysql> SELECT version();
+-----------+
| version() |
+-----------+
| 5.5.28 |
+-----------+
1 row in set (0.00 sec)

为了测试 sql_mode ONLY_FULL_GROUP_BY,我创建了包含两列 id, name 的表 patient 并插入了记录。请记住 sql_mode ONLY_FULL_GROUP_BY 不是默认设置,如果需要,您需要设置。

1)MySQL版本5.0.45-community-nt

SELECT name, MAX(id) FROM patient;
ERROR 1140 (42000): Mixing of GROUP columns (MIN(),MAX(),COUNT(),...) with no GROUP columns is illegal if there is no GROUP BY clause

它失败了,将 sql_mode 设置为 ONLY_FULL_GROUP_BY 没有意义,因为它不允许未在 GROUP BY 子句中命名的非聚合列。

2)MySQL版本5.1.40-community

mysql> SELECT name, MAX(id) from patient;
+----------+--------+
| MAX(id) | name |
+----------+--------+
| 33 | aniket |
+----------+--------+
1 row in set (0.03 sec)

然后设置sql_mode ONLY_FULL_GROUP_BY

mysql> set sql_mode = 'ONLY_FULL_GROUP_BY';
Query OK, 0 rows affected (0.00 sec)

mysql> SELECT name, MAX(id) from patient;
ERROR 1140 (42000): Mixing of GROUP columns (MIN(),MAX(),COUNT(),...) with no GROUP columns is illegal if there is no GROUP BY clause

3)MySQL版本5.5.28

mysql> SELECT name, MAX(id) from patient;
+----------+--------+
| MAX(id) | name |
+----------+--------+
| 33 | aniket |
+----------+--------+
1 row in set (0.03 sec)

然后设置sql_mode ONLY_FULL_GROUP_BY

mysql> set sql_mode = 'ONLY_FULL_GROUP_BY';
Query OK, 0 rows affected (0.00 sec)

mysql> SELECT name, MAX(id) from patient;
ERROR 1140 (42000): Mixing of GROUP columns (MIN(),MAX(),COUNT(),...) with no GROUP columns is illegal if there is no GROUP BY clause

结论

如您所见,查询在版本 5.0.45 上失败,并在 5.1.40、5.5.28 和 5.1.32 上/之后成功(正如您在问题中提到的)。MySQL 版本之前 5.1.10(not sure)无论是否设置了 sql_mode ONLY_FULL_GROUP_BY,没有 GROUP BY 的查询都会失败。

一些有趣的错误和 sql_mode 常见问题解答链接

  1. ONLY_FULL_GROUP_BY sql mode is overly restrictive
  2. sql-mode: only full group by mode not working
  3. MySQL 5.0 FAQ: Server SQL Mode

关于mysql - ONLY_FULL_GROUP_BY 未设置但仍然有错误 1140,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15970686/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com