原表
  name               class  
-----------------------  
  a1                 1  
  a2                 1                  
  a3                 1 
-----------------------                 
  a4                 2                  
  a5                 2 
-----------------------  
  a6                 3 
  a7                 3               分组排序后
 按照class的分组随机排序后  在进行name的随机排序。
以下是效果之一
  name               class
-----------------------
  a5                 2
  a4                 2
-----------------------
  a1                 1
  a3                 1
  a2                 1
-----------------------
  a6                 3
  a7                 3谢谢各位了。

解决方案 »

  1.   

    --> 测试数据: [tb]
    if object_id('[tb]') is not null drop table [tb]
    create table [tb] (name varchar(2),class int)
    insert into [tb]
    select 'a1',1 union all
    select 'a2',1 union all
    select 'a3',1 union all
    select 'a4',2 union all
    select 'a5',2 union all
    select 'a6',3 union all
    select 'a7',3--select * from [tb]select * from tb
    order by checksum(rand()*class), checksum(rand(),name)
    /*
    name class
    ---- -----------
    a4   2
    a5   2
    a3   1
    a2   1
    a1   1
    a7   3
    a6   3(7 row(s) affected)
    */drop table tb