SQL筆記(四) 集合運算

2021-08-20 07:57:36 字數 1787 閱讀 6713

交運算差運算

sql作用的關係上的unionintersect、和except運算對應數學集合論中的-運算

select course_id

from

section

where semester = 'fall'

andyear = 2009;

select course_id

from

section

where semester = 'spring'

andyear = 2010;

使用c1和c2分別指代以上查詢結果的兩個關係。

查詢1. 並運算

找出在2023年秋季學期開課,或者在2023年春季學期開課或兩個學期都開課的所有課程:

(select course_id

from

section

where semester = 'fall'

andyear = 2009)

union

(select course_id

from

section

where semester = 'spring'

andyear = 2010);

union運算自動去除重複

使用union all代替union可以保留重複

查詢2. 交運算

找出2023年秋季和2023年春季同時開課的所有課程的集合:

(select course_id

from

section

where semester = 'fall'

andyear = 2009)

intersect

(select course_id

from

section

where semester = 'spring'

andyear = 2010);

intersect運算自動去除重複

使用intersect all代替intersect可以保留重複

某些sql實現,特別是oracle,使用關鍵字minus代替except。

查詢3. 差運算

找出在2023年秋季開課,但不在2023年春季學期開課的所有課程:

(select course_id

from

section

where semester = 'fall'

andyear = 2009)

except

(select course_id

from

section

where semester = 'spring'

andyear = 2010);

except運算自動去除重複

使用except all代替except可以保留重複

redis學習筆記四 集合

1 新增 刪除元素 sadd key member member.srem key member member.新增的時候,如果元素不存在自動建立,如果存在會自動忽略,不進行新增 2 獲取集合中的所有元素 smembers key 3 判斷元素是否在集合中 sismember key member ...

python學習四(集合 字典)

1 通過集合去掉重複的元素 usr bin python coding utf 8 set1 set a a b b c print set1 輸出 新增元素 set1.add d set1.add c 由於重複,無法新增 print set1 set2 set1.copy set1.clear p...

Scala 學習(四) 集合之List

一,簡介 二,不可變list 三,可變listbuffer scala 列表類似於陣列,它們所有元素的型別都相同,但是它們也有所不同 列表是不可變的,值一旦被定義了就不能改變,其次列表 具有遞迴的結構 也就是鏈結表結構 而陣列不是。而listbuffer元素和長度都是可變的。該多用list而不是ar...