集合运算
union、intersect、except对应于∪、∩、-运算
并运算union
union自动去除重复,如果想保留所有重复,则用union all代替union
(select course_id
from section
where semester = 'Fall' and year = 2009)
union
(select course_id
from section
where semester = 'Spring' and year = 2010)
交运算intersect
intersect自动去除重复,如果想保留所有重复,则用intersect all代替intersect
(select course_id
from section
where semester = 'Fall' and year = 2009)
intersect
(select course_id
from section
where semester = 'Spring' and year = 2010)
差运算except/minus
except在操作前自动去除输入重复,如果想保留所有重复,则用except all代替except
(select course_id
from section
where semester = 'Fall' and year = 2009)
except
(select course_id
from section
where semester = 'Spring' and year = 2010)
也可以使用minus
select *
from sc
where cno='003'
minus
select *
from sc
where grade<60;
空值
- 算术运算中,如果算术表达式的任一输入为空,则该算术表达式(+、-、*、/)结果为空
- SQL将涉及空值的任何比较运算结果视为
unknown,这是true和false之外的第三个逻辑值 - 在and or not布尔运算中,只有
true or unknown结果为true,其他涉及unknown的布尔运算结果都为unknown - 如果where子句计算出
false或unknown,都不会被加入到结果集中 - 用
is null试空值,结果为true,is not null,则为false - 某些SQL还允许使用
is unknown和is not unknown来测试一个表达式的结果是否为unknown - 比较两个元组对应属性值时,都是
null,那么判断上是相同的,这与比较运算null = null结果为unknown不一样

本文介绍了SQL中的集合运算union、intersect和except的基本概念,展示了如何使用它们进行数据合并,并强调了空值处理规则,包括算术运算中的unknown状态和isnull/isnotnull的使用。
353

被折叠的 条评论
为什么被折叠?



