select top 6 * from T_EXAM where type=1 and difficulty=1 and distinguish=1 order by newid()
 union all
 select top 2 * from T_EXAM  where type=2 and difficulty=1 and distinguish=1 order by newid() 
 union all
 select top 5 * from T_EXAM  where type=2 and difficulty=2 and distinguish=1 order by newid() 
 union all
 select top 7 * from T_EXAM  where type=2 and difficulty=2 and distinguish=2 order by newid()
加了newid()是不行了。

解决方案 »

  1.   

    lz要的结果什么?
    这样?
    SELECT  *
    FROM    ( SELECT TOP 6
                        *
              FROM      T_EXAM
              WHERE     type = 1
                        AND difficulty = 1
                        AND distinguish = 1
              UNION ALL
              SELECT TOP 2
                        *
              FROM      T_EXAM
              WHERE     type = 2
                        AND difficulty = 1
                        AND distinguish = 1
              UNION ALL
              SELECT TOP 5
                        *
              FROM      T_EXAM
              WHERE     type = 2
                        AND difficulty = 2
                        AND distinguish = 1
              UNION ALL
              SELECT TOP 7
                        *
              FROM      T_EXAM
              WHERE     type = 2
                        AND difficulty = 2
                        AND distinguish = 2
            ) T
    ORDER BY NEWID() 
      

  2.   


    http://bbs.csdn.net/topics/390228324这个贴,版主的一个回复吧。我试了不行。你那样的话只是打乱顺序。而不是取20个不同的结果
      

  3.   


    lz的问题,在http://bbs.csdn.net/topics/390228324帖子的10楼已经解决了哦。