我现在有一个数据库名为jxsk,里面有数据表如下:
Student(S#,Sname,Sage,Ssex) 学生表 
Course(C#,Cname,T#) 课程表 
SC(S#,C#,score) 成绩表 
Teacher(T#,Tname) 教师表现在我要查询:查询没学过“叶平”老师课的同学的学号、姓名;我用多种方法实现了,但在这个过程中我遇到了一个问题:像下面这样写能得到正确结果:
select student.s#,sname from student
 where student.s# not in
(select sc.s# from sc,course,teacher 
 where sc.c#=course.c# 
 and course.t#=teacher.t#
 and teacher.tname='叶平');但我换一种写法为什么就不行了呢??
select student.s#,sname from student,
(select sc.s# from sc,course,teacher 
 where sc.c#=course.c# 
 and course.t#=teacher.t#
 and teacher.tname='叶平')b
where student.s# not in(select s# from b);下面的这种方法我只是生成了一个临时视图,
为什么会被提示,
对象名b无效???
我想知道这个查询语句为什么错了????
很是不解

解决方案 »

  1.   

    b是别名 是前面嵌套表的别名
    not in(select s# from b); 
    这里的b需要引用完整 不能直接用别名
      

  2.   

    select student.s#,sname from student, 
    (select sc.s# from sc,course,teacher 
    where sc.c#=course.c# 
    and course.t#=teacher.t# 
    and teacher.tname='叶平')b 
    where student.s# not in(select s# from b); 这个b 虽然是前面派生表的别名。但是要么用连接要么前面不要子查询,直接用 not in ()形式
      

  3.   


    往简单了说,SQL语法上不支持该引用方式。
      

  4.   


    ---你非要写的话就这样
    select student.s#,sname from student, 
    (select sc.s# from sc,course,teacher 
    where sc.c#=course.c# 
    and course.t#=teacher.t# 
    and teacher.tname='叶平')b 
    where student.s# not in(select s# from select sc.s# from sc,course,teacher 
    where sc.c#=course.c# 
    and course.t#=teacher.t# 
    and teacher.tname='叶平');