MySQL :: MySQL 8.0 Reference Manual :: 5.1.11 Server SQL Modes https://dev.mysql.com/doc/refman/8.0/en/sql-mode.html#sqlmode_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 BY clause nor are functionally dependent on (uniquely determined by) GROUP BY columns.

A MySQL extension to standard SQL permits references in the HAVING clause to aliased expressions in the select list. The HAVING clause can refer to aliases regardless of whether ONLY_FULL_GROUP_BY is enabled.

For additional discussion and examples, see Section 12.20.3, “MySQL Handling of GROUP BY”.

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:

SELECT o.custid, c.name, MAX(o.payment)
FROM orders AS o, customers AS c
WHERE o.custid = c.custid
GROUP BY o.custid;

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 BYclause 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.

You can achieve the same effect without disabling ONLY_FULL_GROUP_BY by using ANY_VALUE() to refer to the nonaggregated column.

The following discussion demonstrates functional dependence, the error message MySQL produces when functional dependence is absent, and ways of causing MySQL to accept a query in the absence of functional dependence.

This query might be invalid with ONLY_FULL_GROUP_BY enabled because the nonaggregated address column in the select list is not named in the GROUP BY clause:

SELECT name, address, MAX(age) FROM t GROUP BY name;

The query is valid if name is a primary key of t or is a unique NOT NULL column. In such cases, MySQL recognizes that the selected column is functionally dependent on a grouping column. For example, if name is a primary key, its value determines the value of address because each group has only one value of the primary key and thus only one row. As a result, there is no randomness in the choice of address value in a group and no need to reject the query.

SELECT @@sql_mode;

ONLY_FULL_GROUP_BY,STRICT_TRANS_TABLES,NO_ZERO_IN_DATE,NO_ZERO_DATE,ERROR_FOR_DIVISION_BY_ZERO,NO_ENGINE_SUBSTITUTION

SHOW GLOBAL VARIABLES LIKE "%mode%";

Variable_name Value
block_encryption_mode aes-128-ecb
gtid_mode OFF
innodb_autoinc_lock_mode 2
innodb_strict_mode ON
offline_mode OFF
rbr_exec_mode STRICT
slave_exec_mode STRICT
sql_mode ONLY_FULL_GROUP_BY,STRICT_TRANS_TABLES,NO_ZERO_IN_DATE,NO_ZERO_DATE,ERROR_FOR_DIVISION_BY_ZERO,NO_ENGINE_SUBSTITUTION
ssl_fips_mode OFF

SELECT * FROM performance_schema.global_variables WHERE VARIABLE_NAME="sql_mode";

查找全局变量的3种sql语句;

User variables are written as @var_name, where the variable name var_name consists of alphanumeric characters, ._, and $. A user variable name can contain other characters if you quote it as a string or identifier (for example, @'my-var'@"my-var", or @`my-var`).

the ONLY_FULL_GROUP_BY SQL mode is enabled (which it is by default) 用户变量的更多相关文章

  1. Hive报错之java.sql.SQLException: Field 'IS_STOREDASSUBDIRECTORIES' doesn't have a default value

    在创建表的时候报出如下错误: hive> create table if not exists testfile_table( > site string, > url string ...

  2. 此操作只能由 SQL Server 中拥有配置数据库读取权限的用户在已加入到某个服务器场的计算机上执行

    错误提示:此操作只能由 SQL Server 中拥有配置数据库读取权限的用户在已加入到某个服务器场的计算机上执行.若要将此服务器连接到服务器场,请使用 SharePoint 产品配置向导,该向导可从 ...

  3. Error updating database. Cause: java.sql.BatchUpdateException: Field 'id' doesn't have a default value

    异常信息 ### Error updating database. Cause: java.sql.BatchUpdateException: Field 'id' doesn't have a de ...

  4. Caused by: java.sql.SQLException: Field 'id' doesn't have a default value

    1.错误描述 org.hibernate.exception.GenericJDBCException: error executing work at org.hibernate.exception ...

  5. sql server登录名、服务器角色、数据库用户、数据库角色、架构区别联系

    原创链接:https://www.cnblogs.com/lxf1117/p/6762315.html sql server登录名.服务器角色.数据库用户.数据库角色.架构区别联系 1.一个数据库用户 ...

  6. java.sql.SQLException: Field 'id' doesn't have a default value

    1:id 列要设置成自增,自动赋值 java.sql.SQLException: Field 'id' doesn't have a default value at com.mysql.jdbc.S ...

  7. 异常:Caused by: java.sql.SQLException: Field 'cust_id' doesn't have a default value

    异常: 由Java.q.L.SqLExpExt引起:字段“CuSTyID”没有默认值 Caused by: java.sql.SQLException: Field 'cust_id' doesn't ...

  8. Caused by: java.sql.SQLException: Field 'category_id' doesn't have a default value

    ### The error may involve com.qingmu.core.dao.ad.ContentDao.insertSelective-Inline ### The error occ ...

  9. Hibernate报错:Caused by: java.sql.SQLException: Field 'ID' doesn't have a default value

    问题一: 报错如下: org.hibernate.id.IdentifierGenerationException: ids for this class must be manually assig ...

随机推荐

  1. quick-cocos2dx-2.2.4环境搭建

    1.Quick-Coco2d-x介绍 Quick-Coco2d-x是Cocos2d-x在Lua上的增强和扩展版本,廖宇雷廖大觉得官方Cocos2d-x的Lua版本不是太好用,于是便在官方Lua版本的基 ...

  2. ntoj 808 蚂蚁的难题(八)

    蚂蚁的难题(八) 时间限制:2000 ms  |  内存限制:65535 KB 难度:5 描述 蚂蚁是一个古玩爱好者,他收藏了很多瓶瓶罐罐. 有一天,他要将他的宝贝们一字排开, 摆放到一个长度为L的展 ...

  3. iOS开发之使用AFN上传图片

    //1.创建管理者对象 AFHTTPSessionManager *manager = [AFHTTPSessionManager manager]; manager.responseSerializ ...

  4. exec系列函数(execl,execlp,execle,execv,execvp)使用

    本节目标: exec替换进程映像 exec关联函数组(execl.execlp.execle.execv.execvp) 一,exec替换进程映像 在进程的创建上Unix采用了一个独特的方法,它将进程 ...

  5. EasyUI 创建Tree

    tree可以被从标记创建.easyui tree应该定义在ul元素中.无序列表ul元素提供了基本tree结构.每一个li元素被产生一个tree节点,子ul元素产生父tree节点.例子:     < ...

  6. STL的map容器将第3个模板参数设为less_equal或greater_equal会怎样?

    最近都在学Linux系统编程,用C就足矣,有段时间没碰C++了,于是实现些算法练手. 实现多项式乘法的时候发现有几项没有合并同类项,最终调试到这一步时发现了问题. res是map类型,用find查找k ...

  7. git 使用手册

    git 作为开源项目首选版本管理软件,必然其优势不容小觑,下面总结一下初次解除用到的一些技能 1. 基本命令介绍 git help branch 查看branch帮助文档 git branch -a ...

  8. MathType怎么编辑双箭头

    很多的数学相关工作者在写文章或论文的时候常常会用到数学公式编辑器.MathType就是一款深受大家欢迎的公式编辑器.很多的用户在使用过程中会用到双箭头符号来表示推理过程,但是怎么编辑又不知道,下面本教 ...

  9. Python爬虫(六)

    源码: import requests import re from my_mysql import MysqlConnect # 获取问答信息 def get_contents(page,heade ...

  10. Laravel5.1 搭建博客 --上传文件及文件管理

    教程源自:Laravel学院 这一节 咱来说说上传文件的功能实现,我们会把上传的文件保存到项目本地,不仅上传 还有删除和预览功能. 1 配置 我们先从配置开始做起,先修改我们自己创建的 blog.ph ...