MySQL报错“Expression #1 of SELECT list is not in GROUP BY clause and contains nonaggre”
执行SQL:
SELECT login_name,login_ip,sex FROM tank_admin GROUP BY login_name ;
这个sql语句违背了sql_mode=only_full_group_by 。
先看看这个sql_mode=only_full_group_by是个什么意思:
(摘自官网文档)
ONLY_FULL_GROUP_BY
Reject queries for which the select list, HAVING condition, or ORDER BY list refer to nonaggregated columns that are neither named in the GROUP BYclause nor are functionally dependent on (uniquely determined by) GROUP BY columns.
As of MySQL 5.7.5, the default SQL mode includes ONLY_FULL_GROUP_BY. (Before 5.7.5, MySQL does not detect functional dependency andONLY_FULL_GROUP_BY is not enabled by default. For a description of pre-5.7.5 behavior, see the MySQL 5.6 Reference Manual.)
A MySQL extension to standard SQL permits references in the HAVING clause to aliased expressions in the select list. Before MySQL 5.7.5, enablingONLY_FULL_GROUP_BY disables this extension, thus requiring the HAVING clause to be written using unaliased expressions. As of MySQL 5.7.5, this restriction is lifted so that the HAVING clause can refer to aliases regardless of whether ONLY_FULL_GROUP_BY is enabled.
For additional discussion and examples, see Section 13.20.3, “MySQL Handling of GROUP BY”.