sql中union all、union、intersect、minus的区别图解,测试

相关文章

  • sql 的 join、left join、full join的区别图解总结,测试,注意事项

1. 结论示意图

sql中union all、union、intersect、minus的区别图解

  • 对于intersectminus,oracle支持,mysql不支持,可以变通(inexists)实现

2.测试

2.1.创建表和数据

-- 建表
drop table if exists student;   -- oralce 不支持 if exists 
create table student (id   int
);
-- 造数据4条
insert into student (id) values (1);
insert into student (id) values (2);
insert into student (id) values (3);
insert into student (id) values (4);-- 查看表数据
select * from student;

在这里插入图片描述

2.2.查询

2.2.1. A

-- A
select * from student where id in (1,2,3);

在这里插入图片描述

2.2.1. B

-- B
select * from student where id in (2,3,4);

在这里插入图片描述

2.2.3. intersect(A ∩ B)。交集。oracle支持,mysql不支持(可以变通实现)

-- intersect(A ∩ B)。交集
select * from student where id in (1,2,3)
intersect
select * from student where id in (2,3,4);
-- 变通实现
select * from student where id in (1,2,3)
and id in (
select id from student where id in (2,3,4));

在这里插入图片描述

2.2.4. minus(A - A ∩ B)。左差集。oracle支持,mysql不支持(可以变通实现)

-- minus(A - A ∩ B)。左差集
select * from student where id in (1,2,3)
minus
select * from student where id in (2,3,4);
-- 变通实现
select * from student where id in (1,2,3)
and id not in (
select id from student where id in (2,3,4));

在这里插入图片描述

2.2.5. minus(A - A ∩ B)。右差集。oracle支持,mysql不支持(可以变通实现)

-- minus(A - A ∩ B)。右差集
select * from student where id in (2,3,4)
minus
select * from student where id in (1,2,3);
-- 变通实现
select * from student where id in (2,3,4)
and id not in (
select id from student where id in (1,2,3));

在这里插入图片描述

2.2.6. union(A ∪ B)。并集(去重)

-- union(A ∪ B)。并集(去重)
select * from student where id in (1,2,3)
union 
select * from student where id in (2,3,4);

在这里插入图片描述

2.2.7. union all(A + B)。和集(不去重)

-- union all(A + B)。和集(不去重)
select * from student where id in (1,2,3)
union all
select * from student where id in (2,3,4);

在这里插入图片描述

2.2.8. (A minus B) union (B minus A)[(A - B) + (B - A)]或 (A union B) minus (A intersect B)[(A ∪ B) - (A ∩ B)] 。A ∩ B在A ∪ B的补集。oracle支持,mysql不支持(可以变通实现)

-- 算法1:`(A minus  B) union (B minus A)`[(A - B) + (B - A)]。A ∩ B在A ∪ B的补集。
(
select * from student where id in (1,2,3)
minus
select * from student where id in (2,3,4)
)
union 
(
select * from student where id in (2,3,4)
minus
select * from student where id in (1,2,3)
);
-- 算法1:变通实现
(
select * from student where id in (1,2,3)
and id not in (
select id from student where id in (2,3,4))
)
union 
(
select * from student where id in (2,3,4)
and id not in (
select id from student where id in (1,2,3))
);-- 算法2:`(A union B) minus (A intersect B)`[(A ∪ B) - (A ∩ B)] 
--  `(union) minus (intersect)`[(A ∪ B) - (A ∩ B)]。A ∩ B在A ∪ B的补集。
(
select * from student where id in (2,3,4)
union
select * from student where id in (1,2,3)
)
minus
(
select * from student where id in (2,3,4)
intersect
select * from student where id in (1,2,3)
);
-- 算法2:变通实现
select * from 
(
select * from student where id in (1,2,3)
union 
select * from student where id in (2,3,4)
)
where id not in
(
select id from student where id in (1,2,3)
and id in (
select id from student where id in (2,3,4))
);

在这里插入图片描述

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/41058.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

vue pc端项目el-upload上传图片时加水印

html代码&#xff1a; <a-uploadclass"avatar-uploader"list-type"picture-card":file-list"uploadFileList":custom-request"uploadDoneHandle":before-upload"beforeUpload":remove"removeHandle"v-decorat…

案例21 基于Spring Boot+Redis实现图书信息按书号存储案例

1. 案例需求 基于Spring BootRedis实现图书信息按书号存储和取出功能&#xff0c;数据存储至Redis。 2. 创建Spring Boot项目 创建Spring Boot项目&#xff0c;项目名称为springboot-redis02。 3. 选择依赖 ​ pom.xml文件内容如下所示&#xff1a; <?xml version&quo…

浏览器控制台调试代码和JavaScript控制台方法介绍

浏览器控制台调试代码和JavaScript控制台方法介绍 浏览器控制台调试代码 浏览器控制台&#xff08;Console&#xff09;是浏览器提供的一个开发工具&#xff0c;用于在浏览器中执行和调试 JavaScript 代码。它提供了一个交互式环境&#xff0c;可以输入 JavaScript 代码&#…

Qt:隐式内存共享

隐式内存共享 Many C classes in Qt use implicit data sharing to maximize resource usage and minimize copying. Implicitly shared classes are both safe and efficient when passed as arguments, because only a pointer to the data is passed around, and the data i…

C语言:每日一练(选择+编程)

目录 选择题&#xff1a; 题一&#xff1a; 题二&#xff1a; 题三&#xff1a; 题四&#xff1a; 题五&#xff1a; 编程题&#xff1a; 题一&#xff1a;打印1到最大的n位数 示例1 思路一&#xff1a; 题二&#xff1a;计算日期到天数转换 示例1 思路一&#xf…

【JVM】如何判定一个对象已死以及“标记-清除”、“标记-复制”、“标记-整理”三种垃圾收集算法

文章目录 0、如何判定一个对象的生死&#xff1f;1、上文提到的引用又是什么1、强引用&#xff1a;2、软引用&#xff1a;3、弱引用&#xff1a;4、虚引用&#xff1a; 2、垃圾收集算法1、标记-清除2、标记-复制优化&#xff1a;&#x1f447; 3、标记-整理 0、如何判定一个对象…

Java面向对象程序设计——知识、概念、定义及作用(简答)

​专栏&#xff1a;《Java面向对象程序设计》学习笔记 问题是依据考纲整理的&#xff0c;稍微做了一些补充。大部分答案由GPT生成&#xff0c;部分内容摘选自书本。 内容太多了&#xff0c;目前懒得浓缩精炼了&#xff0c;以后再说吧。 如果有大佬可以帮忙精简一些文字、补充…

R语言实现神经网络(1)

#R语言实现神经网络 library(neuralnet) library(caret) library(MASS) library(vcd) data(shuttle) str(shuttle)#因变量use; table1<-structable(windmagn~use,shuttle) mosaic(table1,shadingT) mosaic(use~errorvis,shuttle) prop.table(table(shuttle$use,shuttle$stab…

计算机网络-物理层(二)- 传输方式

计算机网络-物理层&#xff08;二&#xff09;- 传输方式 串型传输与并行传输 串行传输:是指数据是一个比特一个比特依次发送的&#xff0c;因此在发送端和接收端之间&#xff0c;只需要一条数据传输线路即可 并行传输:是指一次发送n个比特而不是一个比特&#xff0c;因此发送…

【Uniapp】base64图片资源转为本地图片,解决canvas不支持base64问题

通过接口获取到base64类型的二维码&#xff0c;把二维码放到canvas里生成海报 遇到的问题&#xff1a; 在微信小程序开发工具中能够正常显示海报&#xff0c;到真机上测试就无法显示二维码 原因&#xff1a; 因为canvas不支持base64&#xff0c;其次在使用小程序 canvas 的 dr…

异常堆栈缺失与OmitStackTraceInFastThrow

目录 现象原因OmitStackTraceInFastThrow源码层面分析OmitStackTraceInFastThrow阈值是多少源码源代码解释 现象 异常没有堆栈信息。只有短短的异常类信息&#xff0c;例如java.lang.NullPointerException。 完整的异常堆栈示例&#xff1a; java.lang.NullPointerException…

爬虫逆向实战(十六)--某建筑市场平台

一、数据接口分析 主页地址&#xff1a;某建筑市场平台 1、抓包 通过抓包可以发现数据接口是list 2、判断是否有加密参数 请求参数是否加密&#xff1f; 无请求头是否加密&#xff1f; 无响应是否加密&#xff1f; 通过查看“响应”模块可以发现&#xff0c;返回的响应是…

MAUI+Blazor:windows 打包踩坑

文章目录 前言MSIX安装文件如何发布选择Windows平台旁加载自定义签名版本号安装 总结 前言 最近打算研究一下MAUIBlazor&#xff0c;争取在今年年底之前彻底搞懂MAUIBlazor的安装模式&#xff0c; MSIX安装文件 Windows 4种安装程序格式MSI&#xff0c;EXE、AppX和MSIX优缺点…

Java常用API---快速达到Java工作水准系列(1)

目录 1.集合 2.包装类 3.日期处理以及格式化 4.字符串处理类 5.数组 5.BigDecimal 6.Math 1.集合 毋庸置疑&#xff0c;集合在实际项目的使用概率几乎是百分之百。无论是用于数据存储和管理、去重和查找亦或是数据检索和遍历&#xff0c;都离不开集合的使用。任何一个项…

Spring Cloud Gateway系例—参数配置(CORS 配置、SSL、元数据)

一、CORS 配置 你可以配置网关来控制全局或每个路由的 CORS 行为。两者都提供同样的可能性。 1. Global CORS 配置 “global” CORS配置是对 Spring Framework CorsConfiguration 的URL模式的映射。下面的例子配置了 CORS。 Example 77. application.yml spring:cloud:gat…

【【STM32----I2C通信协议】】

STM32----I2C通信协议 我们会发现I2C有两根通信线&#xff1a; SCL和SDA 同步 半双工 带数据应答 支持总线挂载多设备&#xff08;一主多从&#xff0c;多主多从&#xff09; 硬件电路 所有I2C设备的SCL连在一起&#xff0c;SDA连在一起 设备的SCL和SDA均要配置成开漏输出模式 …

5.Vue_Element

文章目录 1 Ajax1.1 Ajax介绍1.1.1 Ajax概述1.1.2 Ajax作用1.1.3 同步异步 1.2 Axios1.2.1 Axios的基本使用1.2.2 Axios请求方法的别名 2 前端工程化2.1 前端工程化特点2.2 Vue项目开发流程 3 Vue组件库Element3.1 Element介绍 1 Ajax 1.1 Ajax介绍 1.1.1 Ajax概述 Ajax: 全…

【Unity游戏开发】基于前缀树的红点系统构思与客户端方案

一、前言 前段时间负责了项目中红点系统的实现,和大家分享一下初期是设计思路 红点系统客户端业务的一般实现过程与方式: 数据管理:首先要在客户端建立一个数据管理系统,用于存储和管理各个业务模块的红点状态。可以是一个中央数据管理器或模块化的数据管理系统,具体根据游…

VUE中babel.config.js配置按需引入

VUE中babel.config.js配置 vue/cli-plugin-babel/preset是一款 babel 插件&#xff0c;它会在编译过程中将 import 引入自动转换为按需引入的方式。 module.exports {presets: [vue/cli-plugin-babel/preset],plugins: [[import, {libraryName: element-ui,libraryDirectory…

系统架构设计师---多媒体技术及其应用

概念 媒体:承载信息的载体。 多媒体:数字、文字、声音、图形、图像和动画等各种媒体的有机组合,并与先进的计 算机、通信和广播电视技术相结合,形成一个可组织、存储、操纵和控制多媒体信息的集成环境和 交互系统。 多媒体技术:以数字化为基础,能够对多…