SQL SERVER: 合并相关操作(Union,Except,Intersect)

SQL SERVER: 合并相关操作(Union,Except,Intersect)

复制代码
use tempdb

create table tempTable1 (id int primary key identity, price int)
create table tempTable2 (id int primary key identity, price int)
insert into tempTable1 select 3 union all select 1 union all select 2 union all select 3 
insert into tempTable2 select 3 union all select 4 union all select 1 union all select 2

select * from temptable1
select * from temptable2

--union会删除重复值,也就是说A和B中重复的行,最终只会出现一次,而union all则会保留重复行。
select * from temptable1
union
select * from temptable2

select * from temptable1
union all
select * from temptable2

--差异(Except)
--就是两个集中不重复的部分。例如
SELECT * from temptable1
except
select * from temptable2

--交集(intersect)
--就是两个集中共同的部分。例如
select * from temptable1
INTERSECT
select * from temptable2

DROP TABLE temptable1
DROP TABLE temptable2
复制代码

 

posted @   davidhou  阅读(209)  评论(0编辑  收藏  举报
编辑推荐:
· go语言实现终端里的倒计时
· 如何编写易于单元测试的代码
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
阅读排行:
· 使用C#创建一个MCP客户端
· 分享一个免费、快速、无限量使用的满血 DeepSeek R1 模型,支持深度思考和联网搜索!
· ollama系列1:轻松3步本地部署deepseek,普通电脑可用
· 基于 Docker 搭建 FRP 内网穿透开源项目(很简单哒)
· 按钮权限的设计及实现
点击右上角即可分享
微信分享提示