尝试模拟交叉路口时出错

Error while trying to mimic intersection

我正在尝试模仿查询(出于学术目的)

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

成功模仿

select t.course_id from section t, section s where s.course_id = t.course_id and
s.semester = 'Spring' and s.year = 2010 and t.semester = 'Fall' and t.year = 2009;

当我尝试这些时,

select t.course_id from section t, section s where s.course_id = t.course_id and
(s.semester, s.year, t.semester,t.year) in ('Spring',2010,'Fall',2009);

error at the parenthesis after in predicate(as per the row and column mentioned in error), error is ORA-00920: invalid relational operator 00920. 00000 - "invalid relational operator"

然后我尝试了

select t.course_id from section t, section s where 
s.course_id = t.course_id and (s.semester,s.year) = ('Spring',2010) 
and (t.semester, t.year) in ('Fall',2009);


select t.course_id from section t, section s where 
s.course_id = t.course_id and ((s.semester,s.year) in ('Spring',2010)) 
and ((t.semester, t.year) = ('Fall',2009));

in= 的不同组合在第一个 in=

后的括号处出现相同的错误

提及 (..) in/= (...) 的属性或使用相同的 table 会导致这个或其他原因吗?

使用 Oracle 12c。

首先停止使用"comma join syntax"。它已经过时,并且有一个名为 JOIN 的伟大继任者。您可以在这里阅读更多内容:INNER JOIN ON vs WHERE clause.

其次,您需要用另一对圆括号将您的值括起来:

SELECT t.course_id 
FROM section t 
JOIN section s 
  ON s.course_id = t.course_id
WHERE (s.semester, s.year, t.semester,t.year) IN (('Spring',2010,'Fall',2009));

SqlFiddleDemo

你可能会问 "Why I need extra round brackets?",考虑 IN 子句中的多个值:

WHERE (col1, col2, col3, col4) IN ((1,2,3,4), (5,6,7,8), (9,10,11,12))

您的困惑可能是由单个值引起的,例如:

WHERE col IN (1,2,3);
<=>
WHERE (col) IN ((1), (2), (3));