Print Page | Close Window

Random Select from Database

Printed From: Web Wiz Forums
Category: General Discussion
Forum Name: Database Discussion
Forum Description: Discussion and chat on database related topics.
URL: https://forums.webwiz.net/forum_posts.asp?TID=6500
Printed Date: 29 March 2026 at 3:41pm
Software Version: Web Wiz Forums 12.08 - https://www.webwizforums.com


Topic: Random Select from Database
Posted By: zMaestro
Subject: Random Select from Database
Date Posted: 17 October 2003 at 7:19am

I have a catalogue containing about 20 items, i list them by:

<%
set RSnav = server.createobject ("ADODB.recordset")
SQLnav = "SELECT * FROM eCatalog;"
RSnav.open SQLnav, Conn, 1, 3

While Not RSnav.EOF
response.write " " & RSnav("ID") & ".<br>"
RSnav.MoveNext
Wend

%>

this code lists all the items in the database, what i want is just to select one item randomly and display it.
so only item like number 5 or item no 9, so as to change the content with every reload.

Thanks in advance.




Replies:
Posted By: Flamewave
Date Posted: 17 October 2003 at 7:49am

You could give them all a unique ID numbering 1 through 20, with no missing numbers, and then have ASP generate a random number between 1 and 20, then use a select statement like this: "select * from eCatalog where CatID = " & intRandomNumber

Otherwise, you can put the recordset into an array, and then do the same thing more or less only this time you would be accessing the array: myArray(intRandomNumber)

For more information on copying a recordset to an array, click the link below.
http://www.w3schools.com/ado/met_rs_getrows.asp - http://www.w3schools.com/ado/met_rs_getrows.asp



-------------
- Flamewave

They say the grass is greener on the other side, but if you really think about it, the grass is greener on both sides.


Posted By: MorningZ
Date Posted: 17 October 2003 at 8:40am

if its a MsSQL you can use the following SPROC
http://www.aspfaqs.com/aspfaqs/ShowFAQ.asp?FAQID=173 - http://www.aspfaqs.com/aspfaqs/ShowFAQ.asp?FAQID=173

Alternative database (like Access)
http://www.aspfaqs.com/aspfaqs/ShowFAQ.asp?FAQID=65 - http://www.aspfaqs.com/aspfaqs/ShowFAQ.asp?FAQID=65



-------------
Contribute to the working anarchy we fondly call the Internet


Posted By: cosmic cat
Date Posted: 17 October 2003 at 7:08pm

A really elegant way to do this if you've got SQL Server 7 and above is:-

SELECT TOP nn * FROM my_table ORDER BY NewId()

(where nn is the number of random items you want and my_table is the table you are retrieving the data from)

This adds a GUID to each row and will return random records without the need for complex stored procs or asp routines.



Posted By: cosmic cat
Date Posted: 17 October 2003 at 7:15pm

I've also found an Access 97/2000 version:-

SELECT TOP 1 id, strText FROM tblMyTable ORDER BY Rnd(id)

This needs an autonumber field, id in the table to work as a seed for the Rnd() function to make the records completely random.



Posted By: MorningZ
Date Posted: 17 October 2003 at 7:41pm
Originally posted by cosmic cat cosmic cat wrote:

A really elegant way to do this if you've got SQL Server 7 and above is:-

SELECT TOP nn * FROM my_table ORDER BY NewId()

(where nn is the number of random items you want and my_table is the table you are retrieving the data from)

This adds a GUID to each row and will return random records without the need for complex stored procs or asp routines.

whoa.. excellent little tidbit there!!  nice



-------------
Contribute to the working anarchy we fondly call the Internet


Posted By: zMaestro
Date Posted: 18 October 2003 at 10:28am
Originally posted by cosmic cat cosmic cat wrote:

I've also found an Access 97/2000 version:-

SELECT TOP 1 id, strText FROM tblMyTable ORDER BY Rnd(id)

This needs an autonumber field, id in the table to work as a seed for the Rnd() function to make the records completely random.

 

I tried this but didn't work:

 

I used this..

<%
Dim Rand, RandS, RandHere
Rand = "SELECT TOP 1 id FROM TableName ORDER BY Rnd(id)"
set RandS = server.createobject("adodb.recordset")
RandS.open Rand, Conn, 3, 1

response.write RandS(0)
%>


the database tested has 7 items... always id # 7 displayed,

everytime the only displayed record is the last one, I refreshed 20 times.. but the last record is the only displayed.

i modified the code to:  ORDER BY Rnd(id) DESC.. always id 6 displayed...

 

 

I'm trying the new posts now



Posted By: zMaestro
Date Posted: 18 October 2003 at 10:48am

 no success yet.. they are too complicated...

i am using access...



Posted By: ljamal
Date Posted: 18 October 2003 at 11:43am
When giving the Random function a seed it will also produce the same number and thus if your random seed is constant (in this case the ID of a record), it will always be sorted the same way. This may be different from the default sort, but the rows will always be in the same order sorted by Rnd(id). Remove the seed and sort by Rnd() and you should get random sorting. I don't use Access so I can't tell you explicitly that it will work.

An alternative would be to count the number of records in the database and use VBScript's Rnd() function to select the random record and then query for the that particular record. This is the method that most Ad program use.



NewID() only works with SQL Server so that would not help you with an Access database.

-------------
L. Jamal Walton

http://www.ljamal.com/" rel="nofollow - L. Jamal Inc : Web/ Print Design and ASP Programming


Posted By: cosmic cat
Date Posted: 18 October 2003 at 12:27pm

I've not tested the Access one either, sorry should have made that clear; it's just a snippet I picked up which looked like it should work. The SQL one works though, I use it a lot for randomly picking out records..

I'm taking a look in Access (2000) now to see what it does. Hmm, seems to always reverse the order of the records, so the top one would always be the last record.

I'll have a play later see if I can make it work!



Posted By: zMaestro
Date Posted: 18 October 2003 at 12:44pm

it worked, thanks to MorningZ post.

 

<%
Dim Rand, RandS, RandHere
Rand = "SELECT id, Make, Model, Image FROM eCatalog "
set RandS = server.createobject("adodb.recordset")
RandS.open Rand, Conn, 3, 1

Dim rndMax
rndMax = CInt(RandS.RecordCount)
RandS.MoveFirst
Randomize Timer
Dim rndNumber
rndNumber = Int(RND * rndMax)
RandS.Move rndNumber

 

RandHere = RandHere & "<img border=""0"" onError=""src='GfX/clear.gif'""  src=""eShop/" & RandS(1) & ".gif""><BR>"
RandHere = RandHere &  "<img border=""0"" src=""eShop/" & RandS(3) & """>"
response.write RandHere
%>

 

 



Posted By: zMaestro
Date Posted: 18 October 2003 at 12:53pm
Originally posted by cosmic cat cosmic cat wrote:

I've not tested the Access one either, sorry should have made that clear; it's just a snippet I picked up which looked like it should work. The SQL one works though, I use it a lot for randomly picking out records..

I'm taking a look in Access (2000) now to see what it does. Hmm, seems to always reverse the order of the records, so the top one would always be the last record.

I'll have a play later see if I can make it work!

 

thanks a lot.. it is working fine with the code i posted.



Posted By: cosmic cat
Date Posted: 18 October 2003 at 6:04pm

Cool

Shame there's not a simpler way in Access like SQL Server though, and when I was digging around in Access earlier, I remembered how much I've forgotten about Access (haven't used it for a couple of years now since we moved to MS SQL Server).



Posted By: Bunce
Date Posted: 18 October 2003 at 9:44pm

I remember testing this with Access a while back and we couldn't get it to reseed on the same connection, or was it a pool of connections....   There was some type of work around but I can't find where I saved it.

The SS one comes in quite handy though.



-------------
There have been many, many posts made throughout the world...
This was one of them.


Posted By: sam98
Date Posted: 23 October 2003 at 8:50am
how can we distribute rows in a database

-------------
http://www.bookreportsrus.com - Book Reports http://www.essayacademy.com - Essays http://www.cheappapers.com - Term Papers


Posted By: sam98
Date Posted: 23 October 2003 at 8:51am
how can we distribute columns in a database

-------------
http://www.bookreportsrus.com - Book Reports http://www.essayacademy.com - Essays http://www.cheappapers.com - Term Papers


Posted By: JoeP
Date Posted: 31 October 2003 at 5:08pm

MS Access 2000 - I use this to post a random quotation on one of my pages from a database of quotations, maybe you can get an idea from it:

Edited - Sorry I noticed answer was already posted above!




Print Page | Close Window

Forum Software by Web Wiz Forums® version 12.08 - https://www.webwizforums.com
Copyright ©2001-2026 Web Wiz Ltd. - https://www.webwiz.net