四种数据库,取随机记录的方法
2024-07-21 02:05:54
供稿:网友
本文来源于网页设计爱好者web开发社区http://www.html.org.cn收集整理,欢迎访问。
mysql:select * from tablename order by rand() limit 10sqlserver:select top 10 * from tablename order by newid()
oracle:使用 dbms random value的一种方法
sql> select *from (select *from t order by dbms_random.value) where rownum<10;
a ---------- 39 101 134 5 83 97 96 140 81
已选择9行。
sql> select *from (select *from t order by dbms_random.value) where rownum<10;
a ---------- 27 118 141 103 128 10 142 68 74 有人说这是一种效率最差的方法,恳请告知比这更好的解决方案!
access(一):
yourstr="*1*3*4*6*12*...."sql="select top 10 * form yourdb where instr('*'&id&'*','"&yourstr&"')<>0
access(二):
<% n=10 ''取任意10条记录 set rs = server.createobject ("adodb.recordset") sql = "select * from table" rs.open sql,conn,1,1 count=rs.recordcount ''记录总数 if count<>empty then randomize for i = 1 to n ''循环n次 num=fix(rnd*count) ''num便是随机产生的记录行数,用fix(),使其不会大于count值。 rs.move num ''移到改随机行 response.write rs(0) ''出该条记录 rs.movefirst ''别忘了再把指针移到第一条 next end if rs.close set rs = nothing%>
sql> select *from (select *from t order by dbms_random.value) where rownum<10;
a ---------- 39 101 134 5 83 97 96 140 81
已选择9行。
sql> select *from (select *from t order by dbms_random.value) where rownum<10;
a ---------- 27 118 141 103 128 10 142 68 74 有人说这是一种效率最差的方法,恳请告知比这更好的解决方案!
access(一):
yourstr="*1*3*4*6*12*...."sql="select top 10 * form yourdb where instr('*'&id&'*','"&yourstr&"')<>0
access(二):
<% n=10 ''取任意10条记录 set rs = server.createobject ("adodb.recordset") sql = "select * from table" rs.open sql,conn,1,1 count=rs.recordcount ''记录总数 if count<>empty then randomize for i = 1 to n ''循环n次 num=fix(rnd*count) ''num便是随机产生的记录行数,用fix(),使其不会大于count值。 rs.move num ''移到改随机行 response.write rs(0) ''出该条记录 rs.movefirst ''别忘了再把指针移到第一条 next end if rs.close set rs = nothing%>