注:所有的下划线+斜体语句都是非必须语句
SQL语句:
Select… From… Where…
并运算(union) / 交运算(intersect) / 差运算(except) all
Select… From… Where…
文字描述:
假设第一个子查询(S-F-W)得到的元组编号是①②⑤,第二个子查询(S-F-W)得到的元组编号是①②④
那么union运算得出的元组就为①②④⑤,intersect运算得出的元组就为①②
而union all运算得出的元组就为①①②②④⑤,也就是会出现重复元组
特殊:
一般来讲,并/差/交运算会自动去重,也就是默认"distinct",而加上all之后就不会
有些DBMS例如Mysql并不支持交运算(intersect)或差运算(except)
示例:
①假定所有学生都有选课,求没学过002号课程的学生学号
Select DISTINCT St From SC
EXCEPT
Select St From SC Where Ct = '002';
或者
Select distinct St From SC SC1
Where not exists(Select * From SC
Where Ct = '002' and St = SC1.St);