Student(S#,Sname,Sage,Ssex) 学生表     
Course(C#,Cname,T#) 课程表     
SC(S#,C#,score) 成绩表     
Teacher(T#,Tname) 教师表  
 查询和“1002”号的同学学习的课程“完全相同”的其他同学学号和姓名

解决方案 »

  1.   

    是不是可以把成绩表中的c#按s#来分组,然后取wm_comcat
    再来对比
      

  2.   

    大概类似于
    SELECT s#,wm_concat(c#) from (SELECT s#,c# FROM 成绩表 ORDER BY c#) GROUP BY s#;
      

  3.   


    with as c1002(
    select c# from sc where s#=1002
    )
    select * from student
    where not exists(
    select * from c1002
    where not exists (
    select * from sc where s#=student.s# and c#=c1002.c#)
    );
      

  4.   

    SQL> select * from sc;
     
       S#    C# SCORE
    ----- ----- -----
     1002     1    30
     1002     2    40
     1001     2    50
     1001     1    70
     1003     1    70
     1004     1    66
     1004     3    67
     1004     2    68
     
    8 rows selected
     
    SQL> 
    SQL> with t1 as (select s#,count(*) cnt from sc where s#<>1001 and c# in (select c# from sc where s#=1001) group by s#),
      2  t2 as (select s#,count(*) cnt from sc where s#<>1001 group by s#)
      3  select distinct t1.s#  from t1,t2 where t1.s#=t2.s# and t1.cnt=t2.cnt and t1.cnt=(select count(*) from sc where s#=1001);
     
       S#
    -----
     1002
     
    SQL> 
    感觉还是写的太丑陋,不过结果倒是正确的
      

  5.   

    还是1楼的想法看着舒服
    SQL> with t1 as (select s#,wm_concat(c#) cserial from (select * from sc order by s#,c#)group by s#)
      2  select b.s# from t1 a,t1 b
      3   where a.s#=1002 and b.s#<>1002 and a.cserial=b.cserial;
     
       S#
    -----
     1001
     
    SQL> 
      

  6.   

    SELECT S# FROM SC WHERE C# IN(SELECT  C# FROM SC WHERE S#='1002') GROUP BY S# HAVING
    COUNT(*)=(SELECT COUNT(*) FROM SC WHERE S#='1002');