期末考试表
课程      任课教师   监考教师  考试教室  编号计算机    张三       张三       101     1
计算机    王五       李四       102     2
计算机    张三       王五       103     3软件工程  钱七       张三       207     4
软件工程  冯九       李四       209     5
软件工程  钱七       陈八       405     6
.
.
.
现在想做一个查询,查询出 同一门课程 的任课教师 不包含在 同一门课程的监考教师中的课程和任课教师?例如:《软件工程》的钱七、冯九就不在《软件工程》的监考教师(张三、李四、陈八)这门考试中。    这问题郁闷很久了,请前辈指点!!

解决方案 »

  1.   

    现在想做一个查询,查询出   同一门课程   的任课教师   不包含在   同一门课程的监考教师中的课程和任课教师? 
    select * from t a
    where not exists(select 1 from t where 课程=a.课程 and 任课教师<>b.监考教师  )
      

  2.   

    select * from t a
    where not exists(select 1 from t where 课程=a.课程 and 任课教师<>b.监考教师  )
      

  3.   

    create table #
    (课程        varchar(10),     任课教师  varchar(10),     监考教师 varchar(10),    考试教室 int,    编号 int)
    insert into #select '计算机'   ,      '张三',               '张三',               101 ,          1   union all
    select '计算机'   ,      '王五',               '李四',               102 ,          2   union all
    select '计算机'   ,      '张三',               '王五',               103 ,          3   union all
    select '软件工程' ,    '钱七'  ,             '张三',               207   ,        4     union all
    select '软件工程' ,    '冯九'  ,             '李四',               209   ,        5     union all
    select '软件工程' ,    '钱七'  ,             '陈八',               405   ,        6 
    select * from # a
    where not  exists(select 1 from # where 课程=a.课程 and 任课教师 in(select 监考教师 from # ) )
    /*
    课程         任课教师       监考教师       考试教室        编号          
    ---------- ---------- ---------- ----------- ----------- 
    软件工程       钱七         张三         207         4
    软件工程       冯九         李四         209         5
    软件工程       钱七         陈八         405         6(所影响的行数为 3 行)
    */
      

  4.   

    select * from # a where 任课教师 in (select 监考教师 from # where 课程=a.课程)
      

  5.   

    select * from t a where not exists(select 1 from t where 课程=a.课程 and 任课教师<>b.监考教师)接分的
      

  6.   

    create table #
    (课程        varchar(10),     任课教师  varchar(10),     监考教师 varchar(10),    考试教室 int,    编号 int)
    insert into #select '计算机'   ,      '张三',               '张三',               101 ,          1   union all
    select '计算机'   ,      '王五',               '李四',               102 ,          2   union all
    select '计算机'   ,      '张三',               '王五',               103 ,          3   union all
    select '软件工程' ,    '钱七'  ,             '张三',               207   ,        4     union all
    select '软件工程' ,    '冯九'  ,             '李四',               209   ,        5     union all
    select '软件工程' ,    '钱七'  ,             '陈八',               405   ,        6 select * from # a
    where not  exists(select 1 from # where 课程=a.课程 and 任课教师= 监考教师 )/*
    课程         任课教师       监考教师       考试教室        编号          
    ---------- ---------- ---------- ----------- ----------- 
    软件工程       钱七         张三         207         4
    软件工程       冯九         李四         209         5
    软件工程       钱七         陈八         405         6(所影响的行数为 3 行)
    */