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萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章