为什么 MySQL 允许“group by"?没有聚合函数的查询?

Why does MySQL allow quot;group byquot; queries WITHOUT aggregate functions?(为什么 MySQL 允许“group by?没有聚合函数的查询?)
本文介绍了为什么 MySQL 允许“group by"?没有聚合函数的查询?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

惊奇——这是一个在 MySQL 中完全有效的查询:

Surprise -- this is a perfectly valid query in MySQL:

select X, Y from someTable group by X

如果您在 Oracle 或 SQL Server 中尝试此查询,您会收到自然错误消息:

If you tried this query in Oracle or SQL Server, you’d get the natural error message:

Column 'Y' is invalid in the select list because it is not contained in 
either an aggregate function or the GROUP BY clause.

那么 MySQL 是如何确定每个 X 显示哪个 Y 的呢?它只是选择一个.据我所知,它只是选择它找到的第一个 Y.基本原理是,如果 Y 既不是聚合函数也不是 group by 子句中,那么在查询中指定select Y"就毫无意义.因此,我作为数据库引擎会返回任何我想要的东西,你会喜欢的.

So how does MySQL determine which Y to show for each X? It just picks one. From what I can tell, it just picks the first Y it finds. The rationale being, if Y is neither an aggregate function nor in the group by clause, then specifying "select Y" in your query makes no sense to begin with. Therefore, I as the database engine will return whatever I want, and you’ll like it.

甚至还有一个 MySQL 配置参数来关闭这种松散".http://dev.mysql.com/doc/refman/5.7/en/sql-mode.html#sqlmode_only_full_group_by

There’s even a MySQL configuration parameter to turn off this "looseness". http://dev.mysql.com/doc/refman/5.7/en/sql-mode.html#sqlmode_only_full_group_by

这篇文章甚至提到了 MySQL 是如何因在这方面不符合 ANSI-SQL 标准而受到批评的.http://www.oreillynet.com/databases/blog/2007/05/debunking_group_by_myths.html

This article even mentions how MySQL has been criticized for being ANSI-SQL non-compliant in this regard. http://www.oreillynet.com/databases/blog/2007/05/debunking_group_by_myths.html

我的问题是:为什么 MySQL 是这样设计的?他们打破 ANSI-SQL 的理由是什么?

My question is: Why was MySQL designed this way? What was their rationale for breaking with ANSI-SQL?

推荐答案

我相信这是为了处理按一个字段分组意味着其他字段也被分组的情况:

I believe that it was to handle the case where grouping by one field would imply other fields are also being grouped:

SELECT user.id, user.name, COUNT(post.*) AS posts 
FROM user 
  LEFT OUTER JOIN post ON post.owner_id=user.id 
GROUP BY user.id

在这种情况下,每个 user.id 的 user.name 将始终是唯一的,因此不需要 GROUP BY 子句中的 user.name 会很方便(尽管,正如您所说,是确定的问题范围)

In this case the user.name will always be unique per user.id, so there is convenience in not requiring the user.name in the GROUP BY clause (although, as you say, there is definite scope for problems)

这篇关于为什么 MySQL 允许“group by"?没有聚合函数的查询?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本站部分内容来源互联网,如果有图片或者内容侵犯您的权益请联系我们删除!

相关文档推荐

Execute complex raw SQL query in EF6(在EF6中执行复杂的原始SQL查询)
Hibernate reactive No Vert.x context active in aws rds(AWS RDS中的休眠反应性非Vert.x上下文处于活动状态)
Bulk insert with mysql2 and NodeJs throws 500(使用mysql2和NodeJS的大容量插入抛出500)
Flask + PyMySQL giving error no attribute #39;settimeout#39;(FlASK+PyMySQL给出错误,没有属性#39;setTimeout#39;)
auto_increment column for a group of rows?(一组行的AUTO_INCREMENT列?)
Sort by ID DESC(按ID代码排序)