是否可以在select,having,ORDER BY中使用 不在group by中的字段?

先结论:
SQL-92标准不允许这样
SQL:1999标准允许,但是多余的字段必须功能性依赖group by中的字段(功能性依赖:由group by字段唯一决定)
mysql: 参数:ONLY_FULL_GROUP_BY
* ONLY_FULL_GROUP_BY is enabled, 符合SQL:1999标准的规则
* ONLY_FULL_GROUP_BY is disabled, mysql对于SQL:1999标准的一个扩展将生效,它会允许select中有额外的字段,值为从每个分组中随机取一个。

测试:
SELECT @@SESSION.sql_mode; -- STRICT_TRANS_TABLES,NO_ENGINE_SUBSTITUTION
set @@SESSION.sql_mode ='ONLY_FULL_GROUP_BY,STRICT_TRANS_TABLES,NO_ENGINE_SUBSTITUTION';

摘自MySQL8.0 Reference Manual的一段:

  • 12.20.3 MySQL Handling of GROUP BY
    SQL-92 and earlier does not permit queries for which the select list, HAVING condition, or ORDER BY list refer to nonaggregated columns that are not named in the GROUP BY clause. For example, this query is illegal in standard SQL-92 because the nonaggregated name column in the select list does not appear in the GROUP BY:
    For the query to be legal in SQL-92, the name column must be omitted from the select list or named in the GROUP BY clause.
    SQL:1999 and later permits such nonaggregates per optional feature T301 if they are functionally dependent on GROUP BY columns: If such a relationship exists between name and custid, the query is legal. This would be the case, for example, were custid a primary key of customers.
    MySQL implements detection of functional dependence. If the ONLY_FULL_GROUP_BY SQL mode is enabled (which it is by default), MySQL rejects queries for which the select list, HAVING condition, or ORDER BY list refer to nonaggregated columns that are neither named in the GROUP BY clause nor are functionally dependent on them.
    If ONLY_FULL_GROUP_BY is disabled, a MySQL extension to the standard SQL use of GROUP BY permits the select list, HAVING condition, or ORDER BY list to refer to nonaggregated columns even if the columns are not functionally dependent on GROUP BY columns. This causes MySQL to accept the preceding query. In this case, the server is free to choose any value from each group, so unless they are the same, the values chosen are nondeterministic, which is probably not what you want. Furthermore, the selection of values from each group cannot be influenced by adding an ORDER BY clause. Result set sorting occurs after values have been chosen, and ORDER BY does not affect which value within each group the server chooses. Disabling ONLY_FULL_GROUP_BY is useful primarily when you know that, due to some property of the data, all values in each nonaggregated column not named in the GROUP BY are the same for each group.
posted @ 2020-03-13 21:43  番茄瓜园  阅读(924)  评论(0编辑  收藏  举报