select * from t1 a where 科目= '英语 '  
  and not exists(select * from t1 where 姓名=a.姓名 and 科目= '数学 ') 
语句中为什么要加  姓名=a.姓名 。不加可以么/讲解下exists后的()是什么意思。与前边的语句有什么联系么。我有点糊涂了。

解决方案 »

  1.   

    加上姓名=a.姓名
    表示内部表数据的姓名必须与外部表数据的姓名相等
    假设t1是一个选课表,上面的语句表示找出t1中所有选了'英语'但是没有选'数学'的数据。
    姓名 科目
    张三 英语
    李四 英语
    王五 数学
    赵六 语文那么上面的语句的结果就是
    姓名 科目
    张三 英语
    李四 英语而如果不加姓名=a.姓名则没有符合条件的结果
      

  2.   

    create table #temp
    (
       xm  varchar(10),
       km  varchar(10),
    )
    goinsert into #temp select
    '张三','英语' union all select 
    '李四','英语' union all select 
    '王五','数学' union all select 
    '赵六','语文'select * from #temp a where km='英语'   
      and not exists(select * from #temp where xm=a.xm and km='数学')  
    select * from #temp a where km='英语'   
      and not exists(select * from #temp where km='数学')  drop table #temp/*(4 row(s) affected)xm         km         
    ---------- ---------- 
    张三         英语
    李四         英语(2 row(s) affected)xm         km         
    ---------- ---------- (0 row(s) affected)
    */