select * from pe_A where sc_id='1111' and (enroll_time='2005' and NOT EXISTS (select set3 from sc_setstu where sc_id='1111') b  )

解决方案 »

  1.   

    是不是这个意啊:
    select set3 from sc_setstu where sc_id='1111'  and enroll_time='2005'
      

  2.   

    select * from pe_A where sc_id='1111' or (enroll_time='2005' and NOT EXISTS (select set3 from sc_setstu where sc_id='1111'))
      

  3.   

    select * 
    from pe_A 
    where  sc_id='1111'  
           OR (enroll_time='2005' 
               and NOT EXISTS (select set3 from sc_setstu where sc_id='1111')
              )
      

  4.   

    明白,明白,zjcxc(邹建)和 lsxaa(小李铅笔刀)
    select * 
    from pe_A 
    where  sc_id='1111'  
           OR (enroll_time='2005' 
               and NOT EXISTS (select set3 from sc_setstu where sc_id='1111')
              )
      

  5.   

    不要or 啊,sc_id=11111是前提啊!我的数据库中:select set3 from sc_setstu where sc_id='1111'
    得出的结果是0 (set3是int值)我想要的结果是,当set3=0的时候:
    select * from pe_A where sc_id='1111' and enroll_time='2005' 如果它=1就:
    select * from pe_A where sc_id='1111'所以起初我这样写;
    select * from pe_A where sc_id='1111' and (enroll_time='2005' and NOT EXISTS (select set3 from sc_setstu where sc_id='1111'))但是不对!请问错在哪里啊??
      

  6.   

    select * 
    from pe_A 
    where  sc_id='1111'  
           OR (enroll_time='2005' 
               and NOT EXISTS (select set3 from sc_setstu where sc_id='1111')
              )
    我按照你们这样,可是也不行啊 。当set3等于0的时候,找出来的纪录enroll_time什么值都有,并没有对 enroll_time进行筛选啊!~~~
      

  7.   

    怎麽都沒有反映了?
    我是這樣處理的:
    select * 
    from pe_zong 
    where  sc_id='9437420'  
          and (enroll_time='2005' 
               and not EXISTS (select set3 from sc_setstu where sc_id='9437420' and set3=0))是得到我想要的結果了。不過感覺學得比較笨!!!
    沒辦法了。我沒有第二選擇
      

  8.   

    --示例--示例数据
    create table Pe_Zong(Sc_id int,Enroll_time int)
    insert Pe_Zong select 1,2004
    union  all     select 1,2004
    union  all     select 1,2005
    union  all     select 2,2003
    union  all     select 2,2005
    union  all     select 3,2004
    union  all     select 3,2005
    union  all     select 4,2003create table Sc_SetStu(Sc_id int,set3 int)
    insert Sc_SetStu select 1,0
    union  all       select 3,1
    go--查询
    select a.*
    from Pe_Zong a
    left join(
    select a.Sc_id,Enroll_time=max(a.Enroll_time)
    from Pe_Zong a,Sc_SetStu b
    where a.Sc_id=b.Sc_id and b.set3=0
    group by a.Sc_id
    )b on a.Sc_id=b.Sc_id
    where b.Sc_id is null or a.Enroll_time=b.Enroll_time
    go--删除测试
    drop table Pe_Zong,Sc_SetStu/*--测试结果Sc_id       Enroll_time 
    ----------- ----------- 
    1           2005
    2           2003
    2           2005
    3           2004
    3           2005
    4           2003(所影响的行数为 6 行)
    --*/