`
bugyun
  • 浏览: 557280 次
社区版块
存档分类
最新评论

Expression #1 of SELECT list is not in GROUP BY clause and contains [转]

 
阅读更多

转:https://www.cnblogs.com/da19951208/p/9566221.html

 

安装了mysql5.7,用group by 查询时抛出如下异常:

1 Expression #3 of SELECT list is not in GROUP BY clause and contains nonaggregated column 'userinfo.t_long.user_name' which is not functionally dependent on columns in GROUP BY clause; this is incompatible with sql_mode=only_full_group_by

  

原因:

      MySQL 5.7.5和up实现了对功能依赖的检测。如果启用了only_full_group_by SQL模式(在默认情况下是这样),那么MySQL就会拒绝选择列表、条件或顺序列表引用的查询,这些查询将引用组中未命名的非聚合列,而不是在功能上依赖于它们。(在5.7.5之前,MySQL没有检测到功能依赖项,only_full_group_by在默认情况下是不启用的。关于前5.7.5行为的描述,请参阅MySQL 5.6参考手册。)

  执行以下个命令,可以查看 sql_mode 的内容。

  

mysql> SHOW SESSION VARIABLES;
mysql> SHOW GLOBAL VARIABLES;
mysql> select @@sql_mode;

  可见session和global 的sql_mode的值都为: 
  ONLY_FULL_GROUP_BY,STRICT_TRANS_TABLES,NO_ZERO_IN_DATE,NO_ZERO_DATE,ERROR_FOR_DIVISION_BY_ZERO,NO_AUTO_CREATE_USER,NO_ENGINE_SUBSTITUTION

  only_full_group_by说明: 
  only_full_group_by :使用这个就是使用和oracle一样的group 规则, select的列都要在group中,或者本身是聚合列(SUM,AVG,MAX,MIN) 才行,其实这个配置目前个人感觉和distinct差不多的,所以去掉就好 。

  官网摘抄: 

  官网: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 BY clause 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 and ONLY_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, enabling ONLY_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.

  解决:

  执行以下两个命令:

mysql> set global sql_mode='STRICT_TRANS_TABLES,NO_ZERO_IN_DATE,NO_ZERO_DATE,ERROR_FOR_DIVISION_BY_ZERO,NO_AUTO_CREATE_USER,NO_ENGINE_SUBSTITUTION';
mysql> set session sql_mode='STRICT_TRANS_TABLES,NO_ZERO_IN_DATE,NO_ZERO_DATE,ERROR_FOR_DIVISION_BY_ZERO,NO_AUTO_CREATE_USER,NO_ENGINE_SUBSTITUTION';

  这两个命令,去掉 sql_mode 的 ONLY_FULL_GROUP_BY  

 

  mysql 配置信息读取顺序。

  ①ps aux|grep mysql|grep ‘my.cnf’

  ②mysql –help|grep ‘my.cnf’

  /etc/my.cnf, /etc/mysql/my.cnf, /usr/local/etc/my.cnf, ~/.my.cnf 这些就是mysql默认会搜寻my.cnf的目录,顺序排前的优先。mysql按照上面的顺序加载配置文件,后面的配置项会覆盖前面的。

  如果没有该文件可以自定义一个文件。然后回默认读取配置中的内容) 
  查看你需要修改的是哪个配置文件。我只有/etc/my.cnf 只修改这个文件即可

  配置文件my.cnf通常会分成好几部分,如[client],[mysqld], [mysql]等等。MySQL程序通常是读取与它同名的分段部分,例如服务器mysqld通常读取[mysqld]分段下的相关配置项。如果配置项位置不正确,该配置是不会生效的

  参考:https://stackoverflow.com/questions/37951742/1055-expression-of-select-list-is-not-in-group-by-clause-and-contains-nonaggr

分享到:
评论

相关推荐

    mysql遇到Expression.docx

    在MySQL中,当你尝试执行一个包含`GROUP BY`子句的查询时,可能会遇到"Expression #1 of SELECT list is not in GROUP BY clause and contains nonaggregated column"这样的错误。这个错误提示表明你的查询违反了`...

    MySQL5.7中的sql_mode默认值带来的坑及解决方法

    在正常项目开发过程中,如果MySQL版本从5.6升级到5.7版本。作为DBA在考虑数据库版本升级带来的影响时,一般会有几个注意点...ERROR 1055 (42000): Expression #3 of XXXXXX list is not in GROUP BY clause and contain

    MySQL异常:[Err] 1055 – Expression #1 of ORDER BY clause is not in GROUP BY clause and contains nona…

    在MySQL中,错误提示 "[Err] 1055 – Expression #1 of ORDER BY clause is not in GROUP BY clause and contains nonaggregated column…” 指的是在执行包含`GROUP BY`子句的查询时,你试图对未在`GROUP BY`子句中...

    解决大于5.7版本mysql的分组报错Expression #1 of SELECT list is not in GROUP BY clause and contains nonaggregated

    如果`SELECT`列表中的列没有出现在`GROUP BY`子句中,且该列没有被聚合函数(如`SUM`, `AVG`, `MAX`, `MIN`等)处理,MySQL会抛出`Expression #1 of SELECT list is not in GROUP BY clause and contains ...

    MySQL使用中遇到的问题记录

    当MySQL执行GROUP BY查询时,如果SELECT列表中包含未在GROUP BY中指定的非聚合列,且该列未包含在聚合函数(如SUM、AVG等)中,就会报错。解决这个问题的方法是调整`sql_mode`的设置。你可以通过登录MySQL并运行以下...

    springclouddemo

    1. **Eureka**:Eureka是Spring Cloud中的核心组件之一,它作为一个服务注册中心,负责管理各个微服务实例的生命周期。当服务启动时,它们会向Eureka注册,并定时发送心跳来证明其存活状态。同时,其他服务通过...

    微软内部资料-SQL性能优化3

    If the class is not confident in their understanding, review appendix A04_Locking and its accompanying PowerPoint® file. Transactions in SQL Server provide the ACID properties: Atomicity A ...

    MySQL数据表合并去重的简单实现方法

    Syntax error or access violation: 1055 Expression #1 of SELECT list is not in GROUP BY clause and contains nonaggregated column 'XXX.Y.ZZZZ' which is not functionally dependent on columns in GROUP BY ...

Global site tag (gtag.js) - Google Analytics