-

SQL随机提取数据列表(图文教程)

ASP程序里随机提取Access数据库里的数据的SQL语句用法。

案例
代码部分:点击查看案例

call run()
sub run()
  dim i,n
  rs.open "select * from " & db_PREFIX & "articledetail" ,conn,1,1
  n = 18
  '先要判断总记录数是否少于N,若小于N,则有多少取多少 
  if n>rs.recordCount then n=rs.recordCount
  dim ranNum,ranNumList,j
  for i = 1 to n
    Randomize()

    '找不是重复的记录'
    for j=1 to 136
      ranNum = int(rs.recordCount*rnd) '产生一个随机数
      if instr(","& ranNumList &"," , ","& ranNum &",")=false then
        if ranNumList <> "" then ranNumList=ranNumList & ","
        ranNumList=ranNumList & ranNum
        exit for   '退出j循环
      end if
    next

  'response.write(rs.recordCount & " 数字为:" & ranNum & "<br>")

  rs.Move ranNum '移动游标到随机数位置

    call echo("标题",rs("title"))

    if i>1 then
      if i mod 3=0 then
        response.write("</tr></tbody><tbody><tr>")
      end if
    end if
  rs.Move -ranNum
  next:rs.close
end sub