数据库
首页 > 数据库> > SQL集合运算(并、交、差)

SQL集合运算(并、交、差)

作者:互联网

集合运算

unionintersectexcept对应于∪、∩、-运算

并运算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在操作前自动去除输入重复,如果想保留所有重复,则用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)

标签:运算,union,SQL,course,semester,year,集合,id,select
来源: https://blog.csdn.net/jhin_lx/article/details/120721066