集合运算—union(并集)、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的优先级相等

  • 1
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
可以使用Java中的Set集合来实现求解两个集合并集交集差集,具体代码如下: ```java import java.util.*; public class SetOperations { public static void main(String[] args) { Scanner sc = new Scanner(System.in); System.out.print("请输入第一个集合:"); Set<Integer> set1 = new HashSet<>(); String[] s1 = sc.nextLine().split(" "); for (String str : s1) { set1.add(Integer.parseInt(str)); } System.out.print("请输入第二个集合:"); Set<Integer> set2 = new HashSet<>(); String[] s2 = sc.nextLine().split(" "); for (String str : s2) { set2.add(Integer.parseInt(str)); } // 求并集 Set<Integer> unionSet = new HashSet<>(set1); unionSet.addAll(set2); System.out.println("并集为:" + unionSet); // 求交集 Set<Integer> intersectionSet = new HashSet<>(set1); intersectionSet.retainAll(set2); System.out.println("交集为:" + intersectionSet); // 求差集 Set<Integer> differenceSet = new HashSet<>(set1); differenceSet.removeAll(set2); System.out.println("第一个集合与第二个集合差集为:" + differenceSet); differenceSet = new HashSet<>(set2); differenceSet.removeAll(set1); System.out.println("第二个集合与第一个集合差集为:" + differenceSet); } } ``` 在该代码中,我们首先使用`Scanner`类从控制台读取输入的两个集合,然后通过`HashSet`类构造出两个集合对象`set1`和`set2`。接下来,我们分别使用`addAll()`、`retainAll()`和`removeAll()`方法来求出两个集合并集交集差集,并将结果打印输出。
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值