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.pswp.cn/news/41058.shtml
繁體地址,請注明出處:http://hk.pswp.cn/news/41058.shtml
英文地址,請注明出處:http://en.pswp.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…

系統架構設計師---多媒體技術及其應用

概念 媒體:承載信息的載體。 多媒體:數字、文字、聲音、圖形、圖像和動畫等各種媒體的有機組合,并與先進的計 算機、通信和廣播電視技術相結合,形成一個可組織、存儲、操縱和控制多媒體信息的集成環境和 交互系統。 多媒體技術:以數字化為基礎,能夠對多…