ORA-00936:缺少select内部查询的表达式

时间:2015-03-11 01:59:45

标签: sql oracle10g

这是我的代码: 我正在使用oracle 10g

select distinct s1.SSN, s1.S_NAME, s2.SSN, s2.S_NAME
from student s1, student s2, transcript t1
where s1.SSN <> s2.SSN AND
t1.Student_SSN = s1.SSN AND
t1.Grade = 'A' AND 
(select * from transcript t2 where 
t2.Student_SSN = s2.SSN AND t2.Grade='A');

我收到此错误:

  

t2.Student_SSN = s2.SSN AND t2.Grade ='A')*第7行的错误:   ORA-00936:缺少表达

错误是什么? 感谢

2 个答案:

答案 0 :(得分:1)

问题是select *子句中延迟where子查询。但是,您应该学习正确的SQL语法和显式连接。据推测,你打算这样的事情:

select distinct s1.SSN, s1.S_NAME, s2.SSN, s2.S_NAME
from student s1 join
     student s2
     on s1.SSN <> s2.SSN join
     transcript t1
     on t1.Student_SSN = s1.SSN AND
where t1.Grade = 'A' AND 
      exists (select 1
              from transcript t2
              where t2.Student_SSN = s2.SSN AND t2.Grade = 'A'
             );

我不确定这是否完全符合您的要求(问题没有描述)。但这应该可以解决Oracle错误。

答案 1 :(得分:0)

我认为你想要的是以下内容。你似乎试图找到至少有一个A&#39; A&#39;成绩单上的成绩单。使用EXISTS无需使用DISTINCT

SELECT s1.SSN, s1.s_name, s2.SSN, s2.s_name
  FROM student s1 INNER JOIN student s2
    ON s1.SSN < s2.SSN
 WHERE EXISTS ( SELECT 1 FROM transcript t1
                 WHERE t1.student_SSN = s1.SSN
                   AND t1.grade = 'A' )
   AND EXISTS ( SELECT 1 FROM transcript t2
                 WHERE t2.student_SSN = s2.SSN
                   AND t2.grade = 'A' );

我在比较<的两个值时使用<>代替SSN的原因是使用<>将返回带有重复项的行 - 即给定的SSN的值可以出现两次,一次是针对学生#1,一次是针对学生#2。