流金水怎么用:MS SQL随机选择记录的问题

来源:百度文库 编辑:查人人中国名人网 时间:2024/05/06 05:40:20
我想在MS SQL中,随机选择50条记录,其中BID=10条,CID=40条,这个SQL语句怎么写啊
我想在MS SQL中,随机选择50条记录,其中BID=a的选择10条,CID=b的选择40条,这个SQL语句怎么写啊

已经修正了!自己也学习了一把,在外面又加了一层,HOHO

create table #test(tb_id1 int,tb_id2 int)
insert into #test values(101,201)
insert into #test values(102,202)
insert into #test values(103,203)
insert into #test values(104,204)
insert into #test values(105,205)
insert into #test values(106,206)
insert into #test values(107,207)
insert into #test values(108,208)
insert into #test values(109,209)

select result from
(
select top 5 tb_id1 as result,newid() n from #test
union all
select top 8 tb_id2,newid() from #test
)a order by n

结果集:
result
-----------
203
103
101
206
205
102
201
105
104
207
208
204
202

(所影响的行数为 13 行)

SELECT TOP 50 * FROM table_name ORDER BY NEWID()

查查sql联机帮助的随机函数,我忘了,但肯定有