• 软件测试技术
  • 软件测试博客
  • 软件测试视频
  • 开源软件测试技术
  • 软件测试论坛
  • 软件测试沙龙
  • 软件测试资料下载
  • 软件测试杂志
  • 软件测试人才招聘
    暂时没有公告

字号: 小 中 大 | 推荐给好友 上一篇 | 下一篇

四种数据库,取随机记录的方法

发布: 2007-7-02 11:08 | 作者: admin | 来源: | 查看: 36次 | 进入软件测试论坛讨论

领测软件测试网
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%>

文章来源于领测软件测试网 https://www.ltesting.net/


关于领测软件测试网 | 领测软件测试网合作伙伴 | 广告服务 | 投稿指南 | 联系我们 | 网站地图 | 友情链接
版权所有(C) 2003-2010 TestAge(领测软件测试网)|领测国际科技(北京)有限公司|软件测试工程师培训网 All Rights Reserved
北京市海淀区中关村南大街9号北京理工科技大厦1402室 京ICP备10010545号-5
技术支持和业务联系:[email protected] 电话:010-51297073

软件测试 | 领测国际 | ISTQB | ISTQB官网 | TMMi | TMMi认证 | 国际软件测试工程师认证 | 领测软件测试网