Asp+access how to randomly draw records in a database? __ Database

Source: Internet
Author: User
How can asp+access randomly draw records in a database?
Ask for a different smoke each time.
There's a saying on the web. Select top * from Tab_st ORDER by Rnd (ID)

But in this way, each pumping is the same, which makes no sense.
Please advise the more appropriate method.

Simple and efficient: Do not repeat random read database records

Set rs = Server.CreateObject ("Adodb.recordset")
Rs.Open sql,conn,1,1

DIM appeared
Call Disrndrecord (10,rs.recordcount) ' Invoke function to display records at this location

' ############### #SUBS ################
' #DisRndRecord (Disnum,rsbound)
' #参数DisNum: Display quantity
' #参数rsBound: Random number Generation Range
Sub Disrndrecord (Disnum,rsbound)
DIM I,thisrnd
If Rsbound < disnum Then Disnum = Rsbound ' The total number of records is less than the number of record bars to extract
For i = 0 to DisNum-1
Thisrnd = Getrnd (rsbound) ' Gets a random number that is not repeated
Rs. Move (THISRND) ' cursor to random number position number read
Response.Write ("<br> (" &rs ("id") & ")" &rs ("Title")
Rs. Move (-THISRND)
Next
End Sub
' # function Getrnd (bound) returns a random number that is not repeated
' #参数bound: Random Range
Function Getrnd (bound)
DIM Rannum
Randomize ()
Rannum=int (BOUND*RND)
If Instr (appeared, "[" &ranNum&] ") Then ' random number generated
Rannum = Getrnd (bound)
End If
appeared = appeared & "[&ranNum&]" ' record random number that has occurred
Getrnd = Rannum
End Function

Reference: http://www.haishui.net/view.php?id=454 set Rs=server. CreateObject ("Adodb.recordset")
Sql= "SELECT * FROM table"
Rs.Open sql,conn,1,3 ' Open database table
I=1
Dim Webon ()
Do but not Rs. Eof ' Write array
Redim Preserve Webon (i)
Webon (i) =rs ("id")
I=i+1
Rs.movenext
Loop

Randomize ' randomly get records
P=int ((rs.recordcount*rnd) +1) ' generates random numbers in all records
Rndid=webon (P)

Set Rs=conn. Execute ("SELECT * from table where id=" &rndid) "opens related random records according to the array.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.