2017-07-01 54 views
-1

如何使用IN而不是UNION從以下查詢中獲得相同的結果集?SQL-IN而不是UNION

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

union 

select course_id 
from section 
where semester = 'Spring' 
    and year = '2010'; 
+0

用你正在使用的數據庫標記你的問題。 –

+0

只是'SELECT course_id從部分在哪裏(學期='春'AND年='2010')或(學期='秋季和AND年='2009');' – BitAccesser

回答

2

開始or

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

如果course_id可能會超過一個排,然後用select distinctunion自動執行distinct

一些數據庫支持的元組的格式,讓你做:

select course_id 
from section 
where (semester, year) in (('Fall', '2009'), ('Spring', '2010')); 

一些,但不是全部。所以,這可能會或可能不會對您使用的數據庫有效。

+0

我應該使用IN運算符,但! – user3391088

+0

元組格式工作:)。非常感謝 – user3391088

+1

正如評論請標記您的數據庫。該RDBMS的未來讀者可能對元組格式感興趣。 – Parfait