select top 2 * from tb where leibie in (select distinct leibie from tb ) 随机取有点困难,我想想!
用存储过程吧 declare @count1 int declare @count2 int select @count1=count(*) from tb where leibie='故事' select @count2=count(*) from tb where leibie='寓言' declare @a int select @a=rand(@count1) select * from tb where id=@a select @a=rand(@count1) select * from tb where id=@a select @a=rand(@count2) select * from tb where id=@a select @a=rand(@count2) select * from tb where id=@a
说真的我只会这样, (SELECT TOP 2 title, leibie FROM atc WHERE (class_name = '小故事')) UNION (SELECT TOP 2 title, leibie FROM atc WHERE (class_name = '小寓言')) ORDER BY leibie 不能随机。如果类别多了就不好弄。就要union很多次
生成个随机数i,*1000了取整后得j,select * where id=j
select *,newid() as ni into #t from 表 select id,title,leibie from #t where (select count(*) from #t where leibie=a.leibie and ni>=a.ni)<=2 order by leibie
改一点 select *,newid() as ni into #t from 表 select id,title,leibie from #t a where (select count(*) from #t where leibie=a.leibie and ni>=a.ni)<=2 order by leibie
我对SQL不熟悉。请问 select *,newid() as ni into #t from 表 是什么意思特别是 #t
大哥。。也许是我没有说清楚。不是固定要(3,4,6,7)记录。。是要随机取。如果想那么容易我就不问啦
就是两条故事两天寓言。都是随机的。
自己想过可以用联合。但是总觉得不好。如果类别多了就不好了。
随机取有点困难,我想想!
declare @count1 int
declare @count2 int
select @count1=count(*) from tb where leibie='故事'
select @count2=count(*) from tb where leibie='寓言'
declare @a int
select @a=rand(@count1)
select * from tb where id=@a
select @a=rand(@count1)
select * from tb where id=@a
select @a=rand(@count2)
select * from tb where id=@a
select @a=rand(@count2)
select * from tb where id=@a
(SELECT TOP 2 title, leibie
FROM atc
WHERE (class_name = '小故事'))
UNION
(SELECT TOP 2 title, leibie
FROM atc
WHERE (class_name = '小寓言'))
ORDER BY leibie
不能随机。如果类别多了就不好弄。就要union很多次
into #t
from 表 select id,title,leibie
from #t
where (select count(*) from #t where leibie=a.leibie and ni>=a.ni)<=2
order by leibie
select *,newid() as ni
into #t
from 表 select id,title,leibie
from #t a
where (select count(*) from #t where leibie=a.leibie and ni>=a.ni)<=2
order by leibie
select *,newid() as ni
into #t
from 表
是什么意思特别是 #t