[Repost] Use a SQL to get a random record set in the Access database!

xiaoxiao2021-03-06  38

ASP implementation: Use a SQL to get a random record set in the Access database!

For "Random Record Set" with a SQL to get a database "problem there is already a lot of answers online: SQL Server 2000: SELECT TOP N * from tanblename Order by newid () Access Select Top N * from tanblename Order By RND ([one Automatic number field]) Ok! That's talk about this access! Open Access 2003 to create a table T1: ID_ (Auto) with an automatic number field, Content_ is inserted into a few records, click "Query" to create a new query, "Design View", select T1, then select SQL view (this can be written Your own SQL, more habits), ok, write: select top 3 [content_] from [T1] ORDER BY RND ([ID_]) Then run the result, huh remembers what is the result.

Return the SQL view, then run the result, follow the last time! That's right! (Of course, your T1 is only less than 3 records.) Ok, we are engaged in Web development! Write an ASP to see <% DIM Con, RS Set Con = Server.createObject ("AdoDb.Connection") con.connectionstring = "provider = microsoft.jet.Oledb.4.0; data source =" & server.mappath ("* .mdb ") Con.commandtimeout = 20con.CursorLocation = 3con.open set RS = Con.Execute (" SELECT TOP 3 [Content_] from [T1] ORDER BY RND ([ID_]) ") Do While Not Rs. EOF response.write rs.fields ("content _"). Value & "
" rs.movenext loop rs.close set = nothing attention sets set control, running this ASP on Localhost, see Let's go, remember! F5 again! Huh? ~~~~~~~~~~~ That result! This is not what we want! Of course, the RND should execute the Randomize statement before running! But how can it be implemented? Randomize in Access? My Access is not very familiar. I looked help, I only found the Randomize in the VBS ... Solving the problem is the most spiritual! Hurry and go Google! After a long time, there is no good solution. Some simply use the ASP to cycle to find Position, 呸, I spent half a day, I think SQL solve? The Randomize in Access doesn't know how to use, will ASP's basic skills? Open your mind, have! <% DIM CON, MINPID, RS Set Con = Server.createObject ("AdoDb.Connection") Con.comtring = "provider = microsoft.jet.Oledb.4.0; data source =" & server.mappath ("*. Mdb" ) Con.commandtimeout = 20con.CursorLocation = 3con.open randomize set = con?execute ("SELECT TOP 3 [Content_] from [T1] ORDER BY RND (- (ID_ " & rND () & ")) ") Do While Not Rs.eof Response.write Rs.fields (" Content_ "). Value &"
"

转载请注明原文地址:https://www.9cbs.com/read-53937.html

New Post(0)