我有一张表
我进行如下查询时
select top 3 * from grades  where classId not in (select top 6 classId from grades)结果是
排除前六条记录结果不应该是

解决方案 »

  1.   

    select top 6 classId from grades返回12345674,12345675,12345676,12345677,12345678
    select top 3 * from grades where classId not in (select top 6 classId from grades)返回21345675,21345676,21345677
      

  2.   

    结果是正确的 classid有重复的,所以也过滤掉了
      

  3.   

    你都没加order by怎么控制顺序
      

  4.   

    你都没加order by怎么控制顺序
      

  5.   

    你都没加order by怎么控制顺序
      

  6.   

    --需要排完序再Top 6, Top 3也一样
    select top 3 * from grades where classId not in (select top 6 classId from grades order by ....) order by ...
      

  7.   

    我还没懂为什么是那个结果..
    前6条记录没了.就应该是我画圈圈的那3条阿 .
    select top 3 * from grades where classId not in (select top 6 classId from grades)
    我把这个6换成3它就是显示4,5,6那三条.
      

  8.   

    select top 9 * from grades
    except
    select top 6 * from grades
      

  9.   

    很有可能是你查询字段是classid 所以也会根据这个字段来排序的。猜想的哈。自己去瞧瞧
      

  10.   


    select top 3 * from grades 
    where classId 
    not in (select top 6 classId from grades order by 你选择前六的排序字段)
    order by 你选择前三的排序字段--默认asc升序,desc 降序
      

  11.   

    看来楼主没理解排序的含义, 如果你不排序的话, SQL Server会帮你排, 但不保证它按什么字段排序, 也就意味着它随便排了序后, Top 6 和 Top 3 的结果就不是你预料的了, 所以为了得到想要的结果, 你需要自己排序