我试图模仿查询(用于学术目的)
(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);
in
谓词后括号内的错误(按行和 错误提到的列),错误是ORA-00920:无效的关系 操作者 00920. 00000 - “无效的关系运营商”
然后我试了
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/= (...)
的属性是否有限制,或者使用相同的表会导致这个或其他原因?
使用Oracle 12c。
答案 0 :(得分:1)
首先停止使用“逗号连接语法”。它已经过时,并有一个名为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
强>
您可能会问“为什么我需要额外的圆括号?”,请考虑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));