集合运算—union(并集)、intersect(交集)和except(差集)

本文详细介绍了SQL中的集合运算,包括并集(union和union all)、交集(intersect)、差集(except)等基本概念和使用方法。并给出了具体的示例说明如何通过这些运算符连接多个查询结果。

 

一、集合运算的基本格式是:

集合查询1

<集合运算>

集合查询2

[order by ...]

二、集合运算符是对两个集合操作的,两个集合必须具有相同的列数,列具有相同的数据类型(至少能隐式转换的),最终输出的集合的列名由第一个集合的列名来确定。(可以用来连接多个结果);集合运算对行进行比较时,认为两个NULL值相等。

三、union和union all(并集)集合运算

union(并集)集合运算可以将多个查询结果集合并成一个结果集。union(隐含distinct,去除重复)、union all。

--UNION合并两个查询结果集,并且将其中完全重复的数据行合并为一条
select tName,tSex from teacher 
union
select sName,sSex from student
--UNION ALL合并两个查询结果集,返回所有数据,不会去掉重复的数据
select tName,tSex from teacher 
union all
select sName,sSex from student

Union因为要进行重复值扫描,所以效率低,因此如果不是确定要合并重复行,那么就用UNION ALL

四、intersect(交集)集合运算:删除两个集合中的重复行,返回只有在两个集合中都出现的行

--先将其中完全重复的数据行删除,再对两个查询结果集取其交集
select tName,tSex from teacher 
intersect
select sName,sSex from student

ANSI SQL 支持带有all选项的intersect集合运算,但SQL Server 2008现在还不支持all选项。要想查询交集中的所有数据的办法:

复制代码

with intersect_all as
(
    select row_number() over(partition by tName,tSex order by (select 0)) as rowNum,
        tName,tSex from teacher
    intersect
    select row_number() over(partition by sName,sSex order by (select 0)) as rowNum,
        sName,sSex from student
)
select tName,tSex from intersect_all

--备注:在排序函数的over子句中使用order by (select <常量>)用这种方法可以告诉SQL Server不必在意行的顺序

复制代码

五、except(差集)集合运算:先将其中完全重复的数据行删除,再返回只在第一个集合中出现,在第二个集合中不出现的所有行。

select tName,tSex from teacher 
except
select sName,sSex from student

ANSI SQL 支持带有all选项的except集合运算,但SQL Server 2008现在还不支持all选项。要想查询交集中的所有数据的办法:

复制代码

with except_all as
(
    select row_number() over(partition by tName,tSex order by (select 0)) as rowNum,
        tName,tSex from teacher
    except
    select row_number() over(partition by sName,sSex order by (select 0)) as rowNum,
        sName,sSex from student
)
select tName,tSex from except_all

--备注:在排序函数的over子句中使用order by (select <常量>)用这种方法可以告诉SQL Server不必在意行的顺序

复制代码

六、集合运算的优先级:intersect运算比union和except运算的优先级高,而union和except的优先级相等

回答: 要计算两个vector的交集,可以使用STL中的算法容器操作。首先,我们需要包含头文件<set><algorithm>。然后,我们可以使用set容器来存储vector中的元素,因为set会自动去重。接下来,我们可以使用set_intersection算法来计算两个vector的交集,使用set_union算法来计算两个vector的。 下面是一个示例代码,展示了如何计算两个vector的交集: ```cpp #include <iostream> #include <vector> #include <set> #include <algorithm> int main() { int a[6] = {1, 2, 3, 4, 5, 6}; int b[4] = {4, 5, 6, 7}; std::vector<int> vectorA(a, a + 6); std::vector<int> vectorB(b, b + 4); std::set<int> setA(vectorA.begin(), vectorA.end()); std::set<int> setB(vectorB.begin(), vectorB.end()); std::set<int> intersection; std::set_intersection(setA.begin(), setA.end(), setB.begin(), setB.end(), std::inserter(intersection, intersection.begin())); std::set<int> unionSet; std::set_union(setA.begin(), setA.end(), setB.begin(), setB.end(), std::inserter(unionSet, unionSet.begin())); std::cout << "Intersection: "; for (auto it = intersection.begin(); it != intersection.end(); ++it) { std::cout << *it << " "; } std::cout << std::endl; std::cout << "Union: "; for (auto it = unionSet.begin(); it != unionSet.end(); ++it) { std::cout << *it << " "; } std::cout << std::endl; return 0; } ``` 这段代码首先将两个数组转换为vector,然后使用set容器存储vector中的元素。接下来,使用set_intersection算法计算两个set的交集使用set_union算法计算两个set的。最后,通过遍历set来输出交集的元素。 希望这个例子能够帮助你理解如何计算vector的交集
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值