您的位置:首页 > 数据库

SQL语言学习随手记——union、intersect、except

2017-10-23 09:45 190 查看
1.并运算——union

e.p.

列出在2009年秋季开课,或在2010年春季开课或两个学期都开课的所有课程:

(select course_id from section where semester = 'Fall' and year = 2009)

  union

(select course_id from section where semester = 'Spring' and year = 2010);

注:union运算自动去除重复,如果想保留所有重复,就必须用union all 代替 union。

2.交运算——intersect

e.p.

列出在2009年秋季和2010年春季同时开课的所有课程集合:

(select course_id from section where semester = 'Fall' and year = 2009)

 intersect

(select course_id from section where semester = 'Spring' and year = 2010);

注:intersect运算自动去除重复,如果想保留所有重复,就必须用intersect all 代替 intersect。

3.差运算——except

e.p.

列出在2009年秋季学期开课,但不在2010年春季学期开课的所有课程集合:

(select course_id from section where semester = 'Fall' and year = 2009)

 except

(select course_id from section where semester = 'Spring' and year = 2010);

注:except运算自动去除重复,如果想保留所有重复,就必须用except all 代替except。

       except运算——从其第一个输入中输出所有不出现在第二个输入中的元祖,也即它执行集差操作。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  sql 数据库 语言
相关文章推荐