查找出,在学生与课程表中,有记录的学生名单!
其实简单点可以这样写:
select distinct xm from XS,KC,XS_KC where xh=xs.xh and kch=kc.kch;

select xm from XS,KC,XS_KC where xh=xs.xh and kch=kc.kch group by xm;

解决方案 »

  1.   

    从最外层开始执行:
               找出xs的一条记录,然后检查看是否另集合
               (select *                            |
               from kc  //课程表                    |
               where not exists                     |
               (select *                            |整个这部分可以看成集合 
               from xs_kc  //‘学生_课程’表        |
               where xh=xs.xh and kch=kc.kch)       |
               )                                    |
              不存在(或说为空),如果另集合不存在,则返回到记录集,反之则不反回.
              在上面的集合里,同理也是先查出kc的一条记录,然后检查是否另集合
              (select *                             | 
               from xs_kc  //‘学生_课程’表        |这部分可以看成集合
               where xh=xs.xh and kch=kc.kch)       |
              不存在(或说为空),如果另集合不存在,则返回到记录集,反之则不反回.
      

  2.   

    不好意思,先前答错了!
    我们一层层分析,如果我们把
    select *
           from kc  //课程表
           where not exists
           (select *
              from xs_kc  //‘学生_课程’表
              where xh=xs.xh and kch=kc.kch)
    标为集1,那么语句则为:
    select xm    //‘姓名’字段
    from xs  //‘学生’表
    where not exists
    (
       集1
    );
    那么语句解释为:选出集1中不存在信息的学生姓名名单。
    首先假设取到一个学生信息,该学生的xh为8,则集1为:
    select *
           from kc  //课程表
           where not exists
           (select *
              from xs_kc  //‘学生_课程’表
              where xh= 8 and kch=kc.kch)
    ;
    那么这段话的意思,就是:选出xh为8的学生,kc表在xs_kc表中不存在的所以课程记录。如果有这样的记录,集1为真,否则为假。
    那么现在:
    select xm    //‘姓名’字段
    from xs  //‘学生’表
    where not exists
    (
       集1
    );
    的意思就是:
    如果xh为8的学生,如果集1为真,则不显示,否则显示。那么集1什么时候为假呢?
    我们分析一下“选出xh为8的学生,kc表在xs_kc表中不存在的所以课程记录。”,只有xh为8的学生,在kc表在xs_kc表中都有记录的时候,集1才为假(没有数据),即xh为8的学生,选择了所有的课程。