the ONLY_FULL_GROUP_BY SQL mode is enabled (which it is by default) 用户变量
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
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 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.
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 @
, where the variable name var_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) 用户变量的更多相关文章
- 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 ...
- 此操作只能由 SQL Server 中拥有配置数据库读取权限的用户在已加入到某个服务器场的计算机上执行
错误提示:此操作只能由 SQL Server 中拥有配置数据库读取权限的用户在已加入到某个服务器场的计算机上执行.若要将此服务器连接到服务器场,请使用 SharePoint 产品配置向导,该向导可从 ...
- 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 ...
- 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 ...
- sql server登录名、服务器角色、数据库用户、数据库角色、架构区别联系
原创链接:https://www.cnblogs.com/lxf1117/p/6762315.html sql server登录名.服务器角色.数据库用户.数据库角色.架构区别联系 1.一个数据库用户 ...
- 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 ...
- 异常: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 ...
- 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 ...
- 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 ...
随机推荐
- 模板中的 TemplateBinding 问题
昨天一个朋友向我求助一个自定义水印控件绑定的问题,问题出在文本框中输入的文本,不能绑定到 相应的依赖属性上(自定义的依赖属性 PassText),他纠结了很久找不出问题所在.问题帮他解决后,这里稍 做 ...
- CSS 命名规范总结
CSS 命名规范总结 BEM 命名给 CSS 以及 html 提供清晰结构,命名空间提供更多信息,模块化提高代码的重用,以达到 CSS 命名语义化.可重用性高.后期维护容易.加载渲染快的要求. 首先, ...
- 在无法单步调试的情况下找Bug的技巧
比如说你有一个大的模块A,其组成部分有B,C,D这3个小的模块,现在A出了一个BUG,因为某种原因的限制你无法单步调试.怎么较快地定位BUG发生的根源? 这里记录一下刚才我在找BUG的时候采用的思路, ...
- JS 遍历 json key ,获取设置可变的key
$(rec.data[id]).each(function(){ for(var key in this){ if(key == value){ console.info(this[key].desc ...
- ar命令学习
之前,学习Linux下使用静态库的时候涉及到了这三个命令: gcc -c my_strcpy.c my_strcmp.car rcs libmylib.a *.ogcc -o main main.c ...
- linux 驱动cc1101
cc110x.h /* * File: cc110x.h * Author: elinux * * Created on 2015年4月7日, 上午10:32 */ #ifndef CC110X ...
- 查看、分析memcached使用状态
访问量上升,数据库压力大,怎么办?好办法是在中间挡一层缓存!这个缓存要求高效,不能比数据库慢,否则服务质量受影响:如果能把数据用hash打散存储到硬盘,也是可以的,不过在内存越来越便宜的今天,还是使用 ...
- asp.net 下载的几种方式
protected void Button1_Click(object sender, EventArgs e) { /* 微软为Response对象提供了一个新的方法TransmitFile来 ...
- Spring mvc 返回JSON 在IE 下提示下载 解决办法
http://www.blogjava.net/iamlibo/archive/2013/11/21/406646.html ————————————————————————————————————— ...
- IOS 中微信 网页授权报 key[也就是code]失效 解决办法
枪魂微信平台ios手机点击返回 网页授权失败,报key失效.已经解决,原因是授权key只能使用一次,再次使用就会失效. 解决办法:第一次从菜单中进行授权时,用session记录key和open_id. ...