当前位置:网站首页>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功能.
边栏推荐
猜你喜欢
Docker安装canal、mysql进行简单测试与实现redis和mysql缓存一致性
Exploring Plain Vision Transformer Backbones for Object Detection 论文阅读笔记
mysql根据多字段分组——group by带两个或多个参数
字符函数和字符串函数
JVS低代码能力简介及功能清单
安装MYSQL遇到问题:write configuration file卡主
MySQL百万数据优化总结 一
Shengxin Weekly Issue 38
消息队列面试题(2022最新整理)
[Virtualization Ecological Platform] Platform Architecture Diagram & Ideas and Implementation Details
随机推荐
MySQL面试八股文(2022最新整理)
Redis学习笔记-3.慢查询和其他高级数据结构
MySQL row-level locks (row locks, adjacent key locks, gap locks)
快速学完数据库管理
【Shader】Shader官方示例[通俗易懂]
Is the working process of the belt you know the story - actionreducerstore
列表页优化思路
ApiPost is really fragrant and powerful, it's time to throw away Postman and Swagger
kubernetes之服务发现
SAP 电商云 Spartacus UI 和 Accelerator UI 里的 ASM 模块
imx6ull看门狗使用
最近两个月谷歌 ad 掉的厉害
初识QEMU
jmeter性能测试步骤入门(性能测试工具jmeter)
If the value of the enum map does not exist, deserialization is not performed
学习笔记 Golang 写入文件(io.WriteString、ioutil.WriteFile、file.Write、write.WriteString)
生信周刊第38期
基于C51实现按键控制
file contains vulnerabilities
AWS Amazon cloud account registration, free application for 12 months Amazon cloud server detailed tutorial