如何使用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'));
一些,但不是全部。 因此,这可能适用于也可能不适用于您正在使用的数据库。