如何使用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';
如何使用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';
開始or
:
select course_id
from section
where (semester = 'Fall' and year = '2009') or
(semester = 'Spring' and year = '2010');
如果course_id
可能會超過一個排,然後用select distinct
。 union
自動執行distinct
。
一些數據庫支持的元組的格式,讓你做:
select course_id
from section
where (semester, year) in (('Fall', '2009'), ('Spring', '2010'));
一些,但不是全部。所以,這可能會或可能不會對您使用的數據庫有效。
我應該使用IN運算符,但! – user3391088
元組格式工作:)。非常感謝 – user3391088
正如評論請標記您的數據庫。該RDBMS的未來讀者可能對元組格式感興趣。 – Parfait
用你正在使用的數據庫標記你的問題。 –
只是'SELECT course_id從部分在哪裏(學期='春'AND年='2010')或(學期='秋季和AND年='2009');' – BitAccesser