作者:杨涛涛
资深数据库专家,专研 MySQL 十余年。擅长 MySQL、PostgreSQL、MongoDB 等开源数据库相关的备份恢复、SQL 调优、监控运维、高可用架构设计等。目前任职于爱可生,为各大运营商及银行金融企业提供 MySQL 相关技术支持、MySQL 相关课程培训等工作。
本文来源:原创投稿
比如在MySQL 5.7.x 中,想要实现如下两个需求:
简单创建表t1、表t2,并且插入几条样例数据:
<mysql:5.7.34:(ytt)> create table t1(c1 int);
Query OK, 0 rows affected (0.02 sec)
<mysql:5.7.34:(ytt)> create table t2 like t1;
Query OK, 0 rows affected (0.02 sec)
<mysql:5.7.34:(ytt)> insert t1 values (10),(20),(20),(30),(40),(40),(50);
Query OK, 7 rows affected (0.00 sec)
Records: 7 Duplicates: 0 Warnings: 0
<mysql:5.7.34:(ytt)> insert t2 values (10),(30),(30),(50),(50),(70),(90);
Query OK, 7 rows affected (0.02 sec)
Records: 7 Duplicates: 0 Warnings: 0
<mysql:5.7.34:(ytt)> select * from t1;
+------+
| c1 |
+------+
| 10 |
| 20 |
| 20 |
| 30 |
| 40 |
| 40 |
| 50 |
+------+
7 rows in set (0.00 sec)
<mysql:5.7.34:(ytt)> select * from t2;
+------+
| c1 |
+------+
| 10 |
| 30 |
| 30 |
| 50 |
| 50 |
| 70 |
| 90 |
+------+
7 rows in set (0.00 sec)复制
<mysql:5.7.34:(ytt)> select distinct t1.c1 from t1 join t2 using(c1);
+------+
| c1 |
+------+
| 10 |
| 30 |
| 50 |
+------+
3 rows in set (0.00 sec)复制
<mysql:5.7.34:(ytt)> select distinct t1.c1 from t1 left join t2 using(c1) where t2.c1 is null;
+------+
| c1 |
+------+
| 20 |
| 40 |
+------+
2 rows in set (0.00 sec)复制
创建好同样的表结构和数据,用 intersect 来求交集:
<mysql:8.0.31:(ytt)>table t1 intersect table t2;
+------+
| c1 |
+------+
| 10 |
| 30 |
| 50 |
+------+
3 rows in set (0.00 sec)复制
用 except 来求差集:
<mysql:8.0.31:(ytt)>table t1 except table t2;
+------+
| c1 |
+------+
| 20 |
| 40 |
+------+
2 rows in set (0.00 sec)复制
intersect 和 except 操作符默认去重。比如需要保留原始结果,则可以带上all 关键词:如下求两表差集的结果会保留所有符合条件的记录。
<mysql:8.0.31:(ytt)>table t1 except all table t2;
+------+
| c1 |
+------+
| 20 |
| 20 |
| 40 |
| 40 |
+------+
4 rows in set (0.00 sec)复制
本文关键字:#intersect# #except# #mysql 8.0新特性#
文章推荐:
技术分享 | MySQL Shell 定制化部署 MySQL 实例
新特性解读 | MySQL 8.0 对 GTID 的限制解除
技术分享 | MySQL InnoDB Cluster Set 介绍
技术分享 | 调整 max-write-buffer-size 优化 pika 性能10倍的案例
爱可生开源社区的 SQLE 是一款面向数据库使用者和管理者,支持多场景审核,支持标准化上线流程,原生支持 MySQL 审核且数据库类型可扩展的 SQL 审核工具。
类型 | 地址 |
---|---|
版本库 | https://github.com/actiontech/sqle |
文档 | https://actiontech.github.io/sqle-docs-cn/ |
发布信息 | https://github.com/actiontech/sqle/releases |
数据审核插件开发文档 | https://actiontech.github.io/sqle-docs-cn/3.modules/3.7_auditplugin/auditplugin_development.html |
更多关于 SQLE 的信息和交流,请加入官方QQ交流群:637150065...

文章转载自爱可生开源社区,如果涉嫌侵权,请发送邮件至:contact@modb.pro进行举报,并提供相关证据,一经查实,墨天轮将立刻删除相关内容。
评论
相关阅读
【专家有话说第五期】在不同年龄段,DBA应该怎样规划自己的职业发展?
墨天轮编辑部
1328次阅读
2025-03-13 11:40:53
MySQL8.0统计信息总结
闫建(Rock Yan)
501次阅读
2025-03-17 16:04:03
2月“墨力原创作者计划”获奖名单公布
墨天轮编辑部
470次阅读
2025-03-13 14:38:19
SQL优化 - explain查看SQL执行计划(一)
金同学
398次阅读
2025-03-13 16:04:22
MySQL突然崩溃?教你用gdb解剖core文件,快速锁定“元凶”!
szrsu
378次阅读
2025-03-13 00:29:43
SQLE 4.0 正式版发布,新增 SQL 重写、SQL 性能追踪、语法知识图谱等功能
爱可生开源社区
367次阅读
2025-03-07 10:30:00
MySQL生产实战优化(利用Index skip scan优化性能提升257倍)
chengang
331次阅读
2025-03-17 10:36:40
MySQL数据库当前和历史事务分析
听见风的声音
307次阅读
2025-04-01 08:47:17
MySQL 生产实践-Update 二级索引导致的性能问题排查
chengang
253次阅读
2025-03-28 16:28:31
墨天轮个人数说知识点合集
JiekeXu
244次阅读
2025-04-01 15:56:03