当前位置:网站首页>The function of SQL GROUP BY dependence
The function of SQL GROUP BY dependence
2022-07-31 12:14:00 【DebugUsery】
SQLThe standard knows an interesting feature,You can cast inGROUP BY
The primary key listed in the clause(或唯一键)any functional dependencies,without the need to explicitly add that functional dependency to GROUP BY
子句中.
这意味着什么呢?Consider this simple pattern:
CREATE TABLE author (
id INT NOT NULL PRIMARY KEY,
name TEXT NOT NULL
);
CREATE TABLE book (
id INT NOT NULL PRIMARY KEY,
author_id INT NOT NULL REFERENCES author,
title TEXT NOT NULL
);
复制代码
To count the number of books by author,We tend to write:
SELECT a.name, count(b.id)
FROM author a
LEFT JOIN book b ON a.id = b.author_id
GROUP BY
a.id, -- Required, because names aren't unique
a.name -- Required in some dialects, but not in others
复制代码
在这种情况下,We have to group by something unique,Because if both authors call John Doe,We still want them to produce different groups.因此,GROUP BY a.id
是一个必然的结果.
We are used to using both at the same timeGROUP BY a.name
,Especially in these dialects that require such,因为我们在SELECT
clause listeda.name
:
- Db2
- Derby
- Exasol
- Firebird
- HANA
- Informix
- Oracle
- SQL Server
But is it really necessary?根据SQL标准,它不是,因为在author.id
和author.name
之间存在着功能依赖.换句话说,对于author.id
的每一个值,恰好有一个author.name
的可能值,或者说author.name
是一个函数.author.id
这意味着,如果我们GROUP BY
两列,Or just the primary key,这并不重要.The result in both cases must be the same,So it is possible:
SELECT a.name, count(b.id)
FROM author a
LEFT JOIN book b ON a.id = b.author_id
GROUP BY a.id
复制代码
哪些SQLThe dialect supports this?
至少有以下SQLThe dialect supports this language feature:
- CockroachDB
- H2
- HSQLDB
- MariaDB
- MySQL
- PostgreSQL
- SQLite
- Yugabyte
值得注意的是,在有GROUP BY
的情况下,MySQLIt was once simply ignored whether a column could be unambiguously predictable.Although the query below is rejected in most dialects,但在MySQL中,在引入ONLY_FULL_GROUP_BY模式之前,It was not rejected:
SELECT author_id, title, count(*)
FROM author
GROUP BY author_id
复制代码
If an author has written more than one book,我们应该为author.title
,显示什么?这没有意义,但MySQLStill used to allow it,and will cast any arbitrary value from the group.
今天,MySQLOnly casting AND is allowedGROUP BY
The clause has a functional relationship to the columns,这是SQL标准所允许的.
优点和缺点
Although a shorter syntax that avoids extra columns may be easier to maintain(如果需要的话,It's easy to cast extra columns),But in production there is a risk of some query outages,i.e. when the underlying constraint is disabled,For example for migration.Although it is unlikely to disable primary keys in a live system,But it's still possible that this could happen,如果没有主键,Queries that were previously valid will no longer be valid,原因与MySQLThe old interpretation is invalid for the same.There are no longer guaranteed functional dependencies.
其他语法
从jOOQ 3.16和#11834开始,将有可能在GROUP BY
The table is referenced directly in the clause,rather than a single column.比如说:
SELECT a.name, count(b.id)
FROM author a
LEFT JOIN book b ON a.id = b.author_id
GROUP BY a
复制代码
The semantics will be:
- If the table has a primary key(无论是否复合),在
GROUP BY
The primary key is used in the clause. - 如果表没有主键,All columns of the table are listed.
由于jOOQ支持的RDBMSNone of this syntax is currently supported,So it's a pure one 合成jOOQ功能.
边栏推荐
猜你喜欢
busybox之reboot命令流程分析
ESP8266-Arduino编程实例-MCP9808数字温度传感器驱动
MySQL百万数据优化总结 一
After class, watching the documentation and walking back to the lab, I picked up the forgotten SQL operators again
Docker practical experience: Deploy mysql8 master-slave replication on Docker
A Week of Wonderful Content Sharing (Issue 14)
deeplab implements its own remote sensing geological segmentation dataset
普林斯顿微积分读本03第二章--编程实现函数图像绘制、三角学回顾
安装MYSQL遇到问题:write configuration file卡主
Docker搭建Mysql主从复制
随机推荐
Power BI----几个常用的分析方法和相适应的视觉对象
Docker搭建Mysql主从复制
关于Mysql数据库的介绍
Hybrid brain-computer interface system based on steady-state visual evoked potentials and attentional EEG
ESP8266-Arduino编程实例-HDC1008温度湿度传感器驱动
生信周刊第38期
vb.net 画曲线
JVS低代码能力简介及功能清单
PAT exam summary (exam experience)
列表页优化思路
Basic use of dosbox [easy to understand]
机器学习基本概念
kubernetes之服务发现
Addition logic for SAP Commerce Cloud Product Review
Candence学习篇(11) allegro中设置规则,布局,走线,铺铜
WebGL给Unity传递参数问题1: Cannot read properties of undefined (reading ‘SendMessage‘)
Wearing detection and action recognition of protective gear based on pose estimation
榕树贷款GPU 硬件架构
JVM 运行时数据区与JMM 内存模型详解
B/S架构模式的一个整体执行流程