mysql – 正常列后跟agregate函数,但不在group by中

我知道只有当包含它们的Group By时,才允许使用聚合函数后面的普通列.
但是,为什么以下工作呢?

mysql> select payee,sum(amount) from checks;  
+---------+-------------+  
| payee   | sum(amount) |  
+---------+-------------+  
| Ma Bell |      893.76 |  
+---------+-------------+  
1 row in set (0.00 sec)  
最佳答案
This behavior is an “extension” to MySql

MySQL extends the use of GROUP BY so that the select list can refer
to nonaggregated columns not named in the GROUP BY clause.

但是,This behavior is actually a configurable setting in MySql

ONLY_FULL_GROUP_BY

Do not permit queries for which the select list or (as of MySQL
5.1.10) HAVING list refers to nonaggregated columns that are not named in the GROUP BY clause.

最好尊重group by并添加所有非聚合列,特别是如果有可能有一天你可能会迁移到打开ONLY_FULL_GROUP_BY的服务器.

转载注明原文:mysql – 正常列后跟agregate函数,但不在group by中 - 代码日志