集合运算
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
不一样