又是随机选题的,循环产生这样的sql: select * from (select top 3 *,newid() as a from yourtable where id=1 order by newid()) b union select * from (select top 3 *,newid() as a from yourtable where id=2 order by newid() ) b ... order by a
create table tb(id int) insert into tb select 1 union all select 1 union all select 1 union all select 1 union all select 1 union all select 2 union all select 2 union all select 2 union all select 2 union all select 2 union all select 2 union all select 2 union all select 3 union all select 3 union all select 4 union all select 4declare @sql varchar(8000) set @sql='select ' select @sql=@sql+' top 3 * from tb where id ='+rtrim(a.id)+' union all select' from (select distinct id from tb) a select @sql=left(@sql,len(@sql)-17) --print @sql exec(@sql)
select * from (select top 3 *,newid() as a from yourtable where id=1 order by newid()) b
union
select * from (select top 3 *,newid() as a from yourtable where id=2 order by newid() ) b
...
order by a
如果是2005,看看title,rank等分组语句的用法,应该能用一条语句解决的
???????什么意思?
insert into tb select 1
union all select 1
union all select 1
union all select 1
union all select 1
union all select 2
union all select 2
union all select 2
union all select 2
union all select 2
union all select 2
union all select 2
union all select 3
union all select 3
union all select 4
union all select 4declare @sql varchar(8000)
set @sql='select '
select @sql=@sql+' top 3 * from tb where id ='+rtrim(a.id)+' union all select' from (select distinct id from tb) a
select @sql=left(@sql,len(@sql)-17)
--print @sql
exec(@sql)