Method for Random access of 10 data entries
Copy codeThe Code is as follows:
Select * from table order by rnd (id)
The id in rnd (id) is the auto-increment field in the table.
Access random display records (not repeated) Solution
Copy codeThe Code is as follows:
<%
'------------------------- Database connection -----------------------
Set objConn = Server. CreateObject ("ADODB. Connection ")
ObjConn. ConnectionString = "Provider = Microsoft. Jet. OLEDB.4.0 ;"&_
"Data Source =" & Server. MapPath ("data. mdb ")
ObjConn. Open
'------------------------- Database connection -----------------------
'------------------------- Retrieve data -----------------------
StrSQL = "SELECT id, DataColumn FROM DataTable"
'SQL statement, search database
Set objRS = Server. CreateObject ("ADODB. Recordset ")
'Create record set
ObjRS. Open strSQL, objConn, 1, 1
'Perform search
Count = objRS. RecordCount
'Get the total number of records
Item = 4
'Number of display records
'------------------------- Retrieve data -----------------------
'-------------------------------------------------------------------------------
Redim a (Item, 2), t (Count)
'Define 2 arrays. array a is used to store records. array t is used to delete selected records.
'---------------------------------------
'Initial array value, which is to be compared with this value after data is inserted.
For each j in t
J = 0
Next
'---------------------------------------
'---------------------------------------
'Random extraction record No.
Randomize timer initialize the random number generator
For j = 1 to Item
K = int (rnd * Count + 1) 'randomly retrieves a record from the total number.
Do while t (k) <> 0' to determine whether the record is already in the array
K = int (rnd * Item + 1)
Loop
T (k) = 1' the k-th record is selected
Next
'--------------------------------------
J = 1: I = 1' define subscript
'--------------------------------------
'Cyclically select some records in the dataset objRS and store them in the array.
Do While Not objRS. Eof
If t (j) = 1 then
A (I, 1) = objRS ("id") 'Record id
A (I, 2) = objRS ("DataColumn") 'Record Content
I = I + 1
End if
J = j + 1
ObjRS. MoveNext
Loop
'--------------------------------------
'-------------------------------------------------------------------------------
'---------------------------- Display content --------------------
For I = 1 to Item
Response. write "no." & a (I, 1) & "<br>"
Response. write "content" & a (I, 2) & "<p>"
Next
'---------------------------- Display content --------------------
'---------------------------
'Release Resources
ObjRs. Close
Set objRs = nothing
ObjConn. Close
Set objConn = nothing
'---------------------------
%>
Mssql random 10 records
Copy codeThe Code is as follows:
Select * from talbe order by newid ()
10 random mysql records
Copy codeThe Code is as follows:
Select * from table order by rand () limit 0, 10
Original please indicate the source www.jb51.net