简体   繁体   English

MySQL : 不在 GROUP BY 中

[英]MySQL : isn't in GROUP BY

The site produces results, but with SELECT COUNT and SELECT query with GROUP BY having two different result counts.该站点产生结果,但 SELECT COUNT 和 SELECT 查询与 GROUP BY 有两个不同的结果计数。 This is likely due to the error that is displaying in phpmyadmin but not on the site.这可能是由于 phpmyadmin 中显示的错误,而不是网站上显示的错误。

The Queries:查询:

SELECT count(DISTINCT `name`) as `numrows` FROM `users` WHERE `verified` = '1'

SELECT `name`, `type`, `language`, `code` FROM `users` WHERE `verified` = '1' GROUP BY `name` ORDER BY `count` DESC LIMIT 0, 25

PhpMyAdmin provides the following error: PhpMyAdmin 提供以下错误:

1055 - 'main.users.type' isn't in GROUP BY 1055 -“main.users.type”不在 GROUP BY 中

When reading MySQL docs, I'm still unclear what it is I have to fix.在阅读 MySQL 文档时,我仍然不清楚我必须修复什么。 I can't seem to grasp this.我似乎无法理解这一点。

You need to have a full group by:您需要通过以下方式拥有一个完整的组:

SELECT `name`, `type`, `language`, `code` 
FROM `users` 
WHERE `verified` = '1' 
GROUP BY `name`, `type`, `language`, `code` 
ORDER BY `count` DESC LIMIT 0, 25

SQL92 requires that all columns (except aggregates) in the select clause is part of the group by clause. SQL92 要求 select 子句中的所有列(聚合除外)都是 group by 子句的一部分。 SQL99 loosens this restriction a bit and states that all columns in the select clause must be functionally dependent of the group by clause. SQL99 稍微放宽了这个限制,并声明 select 子句中的所有列必须在功能上依赖于 group by 子句。 MySQL by default allows for partial group by and this may produce non-deterministic answers, example: MySQL 默认允许部分分组,这可能会产生不确定的答案,例如:

create table t (x int, y int);
insert into t (x,y) values (1,1),(1,2),(1,3);
select x,y from t group by x;
+------+------+
| x    | y    |
+------+------+
|    1 |    1 |
+------+------+

Ie a random y is select for the group x.即为组 x 选择随机 y。 One can prevent this behavior by setting @@sql_mode:可以通过设置@@sql_mode 来防止这种行为:

set @@sql_mode='ONLY_FULL_GROUP_BY';
select x,y from t group by x; 
ERROR 1055 (42000): 'test.t.y' isn't in GROUP BY

The best solution to this problem is, of course, using a complete GROUP BY expression.这个问题的最佳解决方案当然是使用完整的GROUP BY表达式。

But there's another solution that works around the ONLY_FULL_GROUP_BY blocking of the old MySQL extension to GROUP BY .但是还有另一种解决方案可以解决ONLY_FULL_GROUP_BYGROUP BY的旧 MySQL 扩展的阻塞。

SELECT name, 
       ANY_VALUE(type) type,
       ANY_VALUE(language) language,
       ANY_VALUE(code) code 
  FROM users  
 WHERE verified = '1' 
 GROUP BY name 
 ORDER BY count DESC LIMIT 0, 25

ANY_VALUE() explicitly declares what used to be implicit in MySQL's incomplete GROUP BY operations -- that the server can choose, well, any, value to return. ANY_VALUE()显式声明了 MySQL 不完整的GROUP BY操作中曾经隐含的内容——服务器可以选择任何值来返回。

Another solution mentioned multiple times above is to turn off that annoying 'ONLY_FULL_GROUP_BY' eg like in this post: Disable ONLY_FULL_GROUP_BY上面多次提到的另一个解决方案是关闭烦人的“ONLY_FULL_GROUP_BY”,例如在这篇文章中: 禁用 ONLY_FULL_GROUP_BY

I think this solution is very useful if you do not want to refactor the whole project for multiple hours.如果您不想在多个小时内重构整个项目,我认为此解决方案非常有用。 And if you do not care about the unpredictable values of the columns which are not list of the GROUP BY .如果您不关心不是GROUP BY列表的列的不可预测值。

First thing is to see why it is like that.第一件事是看看为什么会这样。 Earlier versions allowed us to be negligent a bit: while the values of the grouped column are completely listed (one from each without exceptions) in the result, values of the other selected column(s) are more or less omitted - but the code can't (does not want to) tell which ones on your behalf:早期版本允许我们疏忽一点:虽然分组列的值在结果中完全列出(每个列一个,没有例外),其他选定列的值或多或少被省略 - 但代码可以't(不想)代表你说出哪些:

- Hey, MYSQL, list the families of the street - but add a first name as well to every row.
- Okay sir, but shall I use the father's or the mother's or...? I'm a machine, give exact orders!

Now that we understand the why, we can decide if we have preferences regarding the other column(s).现在我们了解了原因,我们可以决定是否对其他列有偏好。 Use

MAX() AS
MIN() AS
etc, works with strings, too

or if it's really all the same, eg there's no difference between the nonaggregated values that are related to the grouped one, use或者如果它真的完全相同,例如与分组值相关的非聚合值之间没有区别,请使用

ANY_VALUE() AS

Either way, you let mysql know that you're aware of being "equivocal" but you really don't want to focus on all the columns other than the one(s) you grouped.无论哪种方式,您都让 mysql 知道您知道“模棱两可”,但您真的不想关注除您分组的列之外的所有列。

只需禁用查询的严格性。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM