文章目录
- 集合查询
- 并操作union
- 交操作intersect
- 差操作
集合查询
多个select语句的结果可进行集合操作,主要包括:并操作union、交操作intersect和差操作except
并操作union
--查询计算机科学系的学生及年龄不大于十九岁的学生
select *
from Student
where Sdept='CS'
union
select *
from Student
where Sage<=19;
--使用union将多个查询结果合并起来时,系统会自动去掉重复元组。如果要保留重复元组则用union all操作
交操作intersect
--查询计算机科学系的学生与年龄不大于十九岁的学生的交集
select *
from Student
where Sdept='CS'
intersect
select *
from Student
where Sage<=19;
------------------------------
--等价于查询计算机科学系中年龄不大于十九岁的学生
select *
from Student
where Sdept='CS' and Sage<=19
差操作
--查询计算机科学系的学生与年龄不大于十九岁的学生的差集
select *
from Student
where Sdept='CS'
except
select *
from Student
where Sage<=19;
------------------------------
--等价于查询计算机科学系中年龄大于十九岁的学生
select *
from Student
where Sdept='CS' and Sage>19