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

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运算——从其第一个输入中输出所有不出现在第二个输入中的元祖,也即它执行集差操作。



发布了30 篇原创文章 · 获赞 5 · 访问量 6万+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章