()order by rand رکوردها رو به طور تصادفی مرتب میکنه. limit هم فقط 40 تای اول از اونها رو انتخاب میکنه. نتیجه میشه 40 تا رکورد تصادفی از بین کل رکوردها.دوست عزیز اون قسمت Limit چیکار میکنه؟ برام 1کم توضیح بده کد رو لطفـــا!i!
Cls.Sql = "select * from user where city='" + Trim(namecity) + "' and reg='yes' order by RND()"
<%
Randomize()
intRandomNumber = Int(1000*Rnd)+1
strSQL = _
"SELECT TOP 3 TableID, Field1, Rnd(" & -1 * (intRandomNumber) &
"*TableID) " & _
"FROM table1 " & _
"ORDER BY 3"
%>
Sub BuildRandomTable(lngRequest as Long)
Dim dbsRandom As Database
Dim rstOrders As Recordset
Dim rstRandom As Recordset
Dim UpperLimit As Long
Dim LowerLimit As Long
Dim lngCounter As Long
Dim lngGuess As Long
Dim lngRecordCount As Long
' Assumes that this module is in the Northwind database.
Set dbsRandom = CurrentDb
' Delete any existing records from tblRandom table.
dbsRandom.Execute "Delete * from tblRandom;"
' Open Orders as a Table Type recordset.
Set rstOrders = dbsRandom.OpenRecordset("Orders", dbOpenTable)
rstOrders.MoveFirst
LowerLimit = rstOrders!OrderID
rstOrders.MoveLast
UpperLimit = rstOrders!OrderID
lngRecordCount = rstOrders.RecordCount
Set rstRandom = dbsRandom.OpenRecordset("tblRandom", _
dbOpenDynaset)
lngCounter = 1
' Check to make sure the number of
' records requested is reasonable.
If lngRequest > lngRecordCount Then
MsgBox "Request is greater than the total number of records."
Exit Sub
Else
lngRequest = lngRequest + 1
End If
[B][COLOR="Blue"]Randomize[/COLOR][/B]
Do Until lngCounter = lngRequest
' Generate a random number
lngGuess = Int((UpperLimit - LowerLimit + 1) * Rnd + LowerLimit)
' Ensure that it exists in the Orders table.
rstOrders.Index = "PrimaryKey"
rstOrders.Seek "=", lngGuess
If rstOrders.NoMatch Then
' Drop through and generate a new number.
Else
' Check to see if it's already been used in the new table.
rstRandom.FindFirst "lngOrderNumber =" & lngGuess
' If not, add it to the new table.
If rstRandom.NoMatch Then
With rstRandom
.AddNew
!lngGuessNumber = lngCounter
!lngOrderNumber = lngGuess
.Update
End With
lngCounter = lngCounter + 1
End If
End If
Loop
' Clean up.
dbsRandom.Close
End Sub
<%@ Language="VBScript" %>
<% Option Explicit %>
<html>
<head>
<title>Selecting a random record</title>
</head>
<body>
<%
'declare your variables
dim connection, recordset, sConnString, sql
dim intRandomNumber, intTotalRecords, i
'declare SQL statement that will query your database
sql = "SELECT * FROM tblFriends"
'create ADO connection and recordset object
Set connection = Server.CreateObject("ADODB.Connection")
Set recordset = Server.CreateObject("ADODB.Recordset")
'define the connection string, specify database
'driver and the location of database
sConnString="PROVIDER=Microsoft.Jet.OLEDB.4.0;" & _
"Data Source=" & Server.MapPath("Friends.mdb")
'Open the connection to the database
connection.Open(sConnString)
'Open the recordset object executing the SQL
recordset.Open sql, connection, 3, 1
'count the number of records and hold this is the variable intTotalRecords
intTotalRecords = recordset.RecordCount
[COLOR="Blue"][B]Randomize()[/B][/COLOR]
intRandomNumber = Int(intTotalRecords * Rnd)
'move to the random number returned
recordset.Move intRandomNumber
'open the table
Response.write("<table border='1'><tr>")
'loop through the total number of fields
For i = 0 to recordset.Fields.Count - 1
'write out the field value
Response.write("<td>" & recordset(i) & "</td>")
Next
'close the table
response.write("</tr></table>")
'close the recordset and connection objects and free up resources
recordset.Close
Set recordset=Nothing
connection.close
Set connection=Nothing
%>
</body>
</html>
<%
str = ","
str1 = ","
Do Until cnt1 = 0
[COLOR="Blue"][B]Randomize[/B][/COLOR]
RndNumber = Int(Rnd * rndMax)
If (InStr(1, str1, "," & RndNumber & "," ) = 0) Then
str1 = str1 & RndNumber & ","
cnt1 = cnt1 - 1
objRst.MoveFirst
objRst.Move RndNumber
str = str & objRst("id") & ","
End If
Loop
%>
مرسیوالله من که هر چی نگاه میکنم جوابهایی که بهت دادند کامل و کافی هست.
نه کدوم کامل و کافی هست؟
یکی که کدش چند سطر هست رو هم سر در نیاوردم یعنی گذاشتم ولی هیچی انتخاب نمیکرد...
شما خواهشن برام بازنویسیش کن...
ممنون میشم...