摘要:随着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 > 0intersectselect * 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 versionfor the right syntax to use near'intersect select * from new where sushis > 0' at line 1
02
—
EXCEPT
select * from new where tacos > 0exceptselect * from new where sushis > 0;+----+--------+-------+--------+| id | name | tacos | sushis |+----+--------+-------+--------+| 2 | Miguel | 5 | 0 || 5 | Scott | 10 | NULL |+----+--------+-------+--------+
EXCEPT的插图

select * from new where sushis > 0exceptselect * 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进行举报,并提供相关证据,一经查实,墨天轮将立刻删除相关内容。




