access随机选取十条不同记录的问题

frankieyo 2009-09-24 10:16:39
这段代码我觉得很好用,解决了access随即选取n条记录的问题
但是有个问题,如果数据库里面选出来的记录总数,小于要显示数 如Item=10
就会超时

需要加个条件,就是如果数据库选取出来的记录总数 小于 要求显示记录数
那么就以选取出来的总数显示...
拜托了,我实在不清楚加在哪




'执行检索
Count=rs.RecordCount
'得到记录总数
Item=10
'显示记录数

redim a(Item, 2),t(Count)
'定义2数组,数组a用来储存记录,数组t用来删选记录

'初始数组数值,目的为了插入数据以后和此值做比较
for each j in t
j=0
next

' 随机抽取记录号
Randomize timer '初始化随机数生成器
for j=1 to Item
k=int(rnd*Count+1) '从总数里面随机取一条记录
do while t(k)<>0 '判断是否记录是否已经在数组中
k=int(rnd*Item+1)
loop
t(k)=1 '第k条记录被选中
next
'--------------------------------------
j=1:i=1'定义下标

Do While Not rs.Eof
if t(j)=1 then
a(i,1)=rs("id") '记录id
a(i,2)=rs("s_name") '记录内容
i=i+1
end if
j=j+1
rs.MoveNext

Loop


for i=1 to Item
Response.write "序号"&a(i,1)&"<br>"
Response.write "内容"&a(i,2)&"<p>"
next
...全文
347 14 打赏 收藏 转发到动态 举报
写回复
用AI写文章
14 条回复
切换为时间正序
请发表友善的回复…
发表回复
  • 打赏
  • 举报
回复
<%
'On Error Resume Next

'连接数据库
dim db,conn,rs
db = "provider=microsoft.jet.oledb.4.0;data source=" & server.mappath("./db.mdb")
set conn = server.createobject("adodb.connection")
set rs = server.createobject("adodb.recordset")
conn.open db
dim timestr,sqlstr
Randomize
timestr = second(now())*second(now())* second(now())
timestr = rnd(timestr)
timestr = "0" & timestr
timestr = 100000 * timestr
Set rs=server.CreateObject("adodb.recordset")
sql = "select top 10 * from table1 order by " & timestr & " mod id,id desc"
rs.open sql,conn,1,1
If not Rs.BOF Then
do while not rs.eof
response.Write rs("id")&":"&rs("aaaa")&"<br>"
rs.movenext
loop
end if
%>
<%
'释放资源
conn.close
set conn = nothing
%>

这是测试程序
  • 打赏
  • 举报
回复
dim timestr,sqlstr 
Randomize
timestr = second(now()) * second(now()) * second(now()) timestr = rnd(timestr) timestr = "0" & timestr
timestr = 100000 * timestr
sqlstr = "select top 10 * from table order by " & timestr & " mod id"
dgboby 2009-09-26
  • 打赏
  • 举报
回复
说其实,直接TOP 10 就行,

数据库不够10 DATAS,就是全部取出,
多于就取10条了.
河南浪人 2009-09-26
  • 打赏
  • 举报
回复
学习了
frankieyo 2009-09-26
  • 打赏
  • 举报
回复
没有人看到我的后续问题啊
Dogfish 2009-09-25
  • 打赏
  • 举报
回复
http://topic.csdn.net/u/20090922/00/bdb49308-b419-4414-9e79-739efaf91e4d.html
上面的那个帖子的楼主可以在access的sql语句里完成。

楼主可以参考一下。
hj3793 2009-09-25
  • 打赏
  • 举报
回复
select top 10 * from myTable Order By Ran()
黑心 2009-09-25
  • 打赏
  • 举报
回复
access没有NewID()
这个东西。
bo3235 2009-09-25
  • 打赏
  • 举报
回复
用数据库方式直接实现


select top 10 * from myTable Order By NewID()
zzhqiao 2009-09-25
  • 打赏
  • 举报
回复
[Quote=引用 1 楼 hjx398 的回复:]
VBScript code'执行检索Count=rs.RecordCount'得到记录总数Item=10if Count< Itemthen
Item=Countendif
[/Quote]同意
zhangjiewoshiwo 2009-09-25
  • 打赏
  • 举报
回复
照上面那样写应该是不会占资源的,要说是很占资源我觉得应该是你写的那个随机抽取10条记录的占点资源,个人感觉。。。。我试过!LZ可以参考下改改!
frankieyo 2009-09-25
  • 打赏
  • 举报
回复
[Quote=引用 1 楼 hjx398 的回复:]
VBScript code'执行检索Count=rs.RecordCount'得到记录总数Item=10if Count< Itemthen
Item=Countendif
[/Quote]

非常感谢,问题解决
有个新问题,这样执行以后,有明显停顿的感觉,是否是这样写太占用资源???
frankieyo 2009-09-25
  • 打赏
  • 举报
回复
[Quote=引用 2 楼 dogfish 的回复:]
http://topic.csdn.net/u/20090922/00/bdb49308-b419-4414-9e79-739efaf91e4d.html
上面的那个帖子的楼主可以在access的sql语句里完成。

楼主可以参考一下。
[/Quote]


您好,这个试过了,现在用的也是这个
但是有个问题,这样生成的数据,靠的非常近,比如132 131 129 127 126 这样,我想是不是随机生成这块有问题?
还有,每刷新一次,不会重新选取记录,这是个问题呢
hjx398 2009-09-24
  • 打赏
  • 举报
回复

'执行检索
Count=rs.RecordCount
'得到记录总数
Item=10
if Count < Item then
Item=Count
end if

28,406

社区成员

发帖
与我相关
我的任务
社区描述
ASP即Active Server Pages,是Microsoft公司开发的服务器端脚本环境。
社区管理员
  • ASP
  • 无·法
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧