摘要:随着MySQL最新版本(8.0.31)的推出,MySQL增加了对SQL标准INTERSECT和EXCEPT运算符的支持。
原文:https://blogs.oracle.com/mysql/post/intersect-and-except-in-mysql-80 (本文被Oracle评为2022年最佳博客)
作者:Frédéric Descamps,Oracle公司MySQL社区经理,知名MySQL布道师 。
关于译者,姚远:
Oracle ACE(Oracle和MySQL数据库方向)
华为云MVP
《MySQL 8.0运维与优化》的作者
中国唯一一位Oracle高可用大师
拥有包括 Oracle 10g和12c OCM在内的20+数据库相关认证。
曾任IBM公司数据库部门经理
现在一家第三方公司任首席数据库专家,服务2万+客户。
CREATE TABLE `new` (
`id` int NOT NULL AUTO_INCREMENT,
`name` varchar(20) DEFAULT NULL,
`tacos` int DEFAULT NULL,
`sushis` int DEFAULT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB
复制
select * from new;
+----+-------------+-------+--------+
| id | name | tacos | sushis |
+----+-------------+-------+--------+
| 1 | Kenny | NULL | 10 |
| 2 | Miguel | 5 | 0 |
| 3 | lefred | 4 | 5 |
| 4 | Kajiyamasan | NULL | 10 |
| 5 | Scott | 10 | NULL |
| 6 | Lenka | NULL | NULL |
+----+-------------+-------+--------+
复制
01
—
INTERSECT
(query 1) select * from new where tacos>0;
(query 2) select * from new where sushis>0;
复制
INTERSECT的插图
select * from new where tacos > 0
intersect
select * from new where sushis > 0;
+----+--------+-------+--------+
| id | name | tacos | sushis |
+----+--------+-------+--------+
| 3 | lefred | 4 | 5 |
+----+--------+-------+--------+
复制
ERROR 1064 (42000): You have an error in your SQL syntax;
check the manual that corresponds to your MySQL server version
for the right syntax to use near
'intersect select * from new where sushis > 0' at line 1
复制
02
—
EXCEPT
select * from new where tacos > 0
except
select * from new where sushis > 0;
+----+--------+-------+--------+
| id | name | tacos | sushis |
+----+--------+-------+--------+
| 2 | Miguel | 5 | 0 |
| 5 | Scott | 10 | NULL |
+----+--------+-------+--------+
复制
EXCEPT的插图
select * from new where sushis > 0
except
select * from new where tacos > 0;
+----+-------------+-------+--------+
| id | name | tacos | sushis |
+----+-------------+-------+--------+
| 1 | Kenny | NULL | 10 |
| 4 | Kajiyamasan | NULL | 10 |
+----+-------------+-------+--------+
复制
03
—
结论
欢迎加我的微信👇
近期热文
想快速学好英语吗?来看看一个IT人是如何达到英语专业八级的水平的。
实际上学好Oracle数据库只需要看一本书,让Oracle ACE来告诉你如何学好Oracle数据库?
刚刚上市10天就卖了一千本,京东和当当都卖断了货!
B站上最火的MySQL性能优化课程。
Linux系统管理录像22讲
零基础Linux课程
点击“在看”可以阅读我翻译的其他文章👇
文章转载自oracleace,如果涉嫌侵权,请发送邮件至:contact@modb.pro进行举报,并提供相关证据,一经查实,墨天轮将立刻删除相关内容。
评论
相关阅读
2025年4月中国数据库流行度排行榜:OB高分复登顶,崖山稳驭撼十强
墨天轮编辑部
1872次阅读
2025-04-09 15:33:27
2025年3月国产数据库大事记
墨天轮编辑部
868次阅读
2025-04-03 15:21:16
2025年3月国产数据库中标情况一览:TDSQL大单622万、GaussDB大单581万……
通讯员
604次阅读
2025-04-10 15:35:48
征文大赛 |「码」上数据库—— KWDB 2025 创作者计划启动
KaiwuDB
497次阅读
2025-04-01 20:42:12
数据库,没有关税却有壁垒
多明戈教你玩狼人杀
486次阅读
2025-04-11 09:38:42
国产数据库需要扩大场景覆盖面才能在竞争中更有优势
白鳝的洞穴
464次阅读
2025-04-14 09:40:20
Oracle SQL 执行计划分析与优化指南
Digital Observer
446次阅读
2025-04-01 11:08:44
墨天轮个人数说知识点合集
JiekeXu
441次阅读
2025-04-01 15:56:03
MySQL数据库当前和历史事务分析
听见风的声音
426次阅读
2025-04-01 08:47:17
最近我为什么不写评论国产数据库的文章了
白鳝的洞穴
405次阅读
2025-04-07 09:44:54