MySQL:GROUP BY上的非聚合字段会怎样? [英] MySQL: What happens to non-aggregated fields upon a GROUP BY?

查看:1158
本文介绍了MySQL:GROUP BY上的非聚合字段会怎样?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我对MySQL中的以下行为有一个非常基本的问题.

I have very a basic question about the following behavior in MySQL.

假设我们执行以下GROUP BY:

SELECT a, b, SUM(c)
FROM table
GROUP BY b;

a字段会发生什么情况,既不聚合也不包含在GROUP BY字段中?

What happens to the field a, which is neither aggregated nor is it included in the GROUP BY fields?

MySQL是否仅将FIRST(a)隐式地应用于a?如果是这样,此行为是一致的还是从a的所有值中获取一个随机值?

Does MySQL just implicitly apply FIRST(a) to a? If so, is this behavior consistent or does it grab a random value out of all values for a?

推荐答案

这是查询处理器从存储介质中获取的第一个结果值,具体取决于所选的查询策略.从技术上讲,这是未定义的,但是您的表除了键之外没有其他索引,这是确定性的,但是您受优化器的支配.

It's the first result value the query processor gets back from the storage medium, dependant on the chosen query strategy. Technically this is undefined, but your table has no indicies other than it's key, this is reasonably deterministic, but you're at the mercy of the optimiser.

依靠它,后果自负.这就是发明窗函数的原因.

Rely on this at your peril. This is why window functions were invented.

这篇关于MySQL:GROUP BY上的非聚合字段会怎样?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆