Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > ASP .Net > ASP General > Select 4 records, Randomly from Database

Reply
Thread Tools

Select 4 records, Randomly from Database

 
 
tamaker
Guest
Posts: n/a
 
      12-12-2007
Is this do-able with ASP / VBscript?

-- I have a database with user records (name, photo, etc. etc.)

I want to use asp to generate (on the homepage) a series of 4 randomly
selected 'user records' from the database -- say just the headshot
photo or name from the database.

In addition to the recordset being randomly generated (i.e. our of
about 50 records, only records 4, 18, 23 and 26 are displayed) I need
to MAKE SURE they are not duplicated (i.e. out of the 50 records,
records 4, 18, 23 and 23 -- where the last two are duplicates).

Is there a way I can pull this off and how do I go about it exactly?


Thanks!

 
Reply With Quote
 
 
 
 
Bruce
Guest
Posts: n/a
 
      12-12-2007
1) get the number of records (users in the database)
2) generate 4 random numbers, make sure that they are not the same, and the
they are not higher than the total number of users (let's say that you got
12,25,28 and 30)
3) choose the highest number that you selecte (among the 4 random numbers)
(that will be 30)
4) select top 30 * from users (assuming that 30 is the highest number). This
will get 30 users from the database
5) loop thru the records, when you're at the random numbers, display them
(meaning that you display only 12,25,28 and 30).

This isn't the most optimized way, but try it and make it work. You can
always tweak it later.

Hope this helps
B

"tamaker" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
> Is this do-able with ASP / VBscript?
>
> -- I have a database with user records (name, photo, etc. etc.)
>
> I want to use asp to generate (on the homepage) a series of 4 randomly
> selected 'user records' from the database -- say just the headshot
> photo or name from the database.
>
> In addition to the recordset being randomly generated (i.e. our of
> about 50 records, only records 4, 18, 23 and 26 are displayed) I need
> to MAKE SURE they are not duplicated (i.e. out of the 50 records,
> records 4, 18, 23 and 23 -- where the last two are duplicates).
>
> Is there a way I can pull this off and how do I go about it exactly?
>
>
> Thanks!
>



 
Reply With Quote
 
 
 
 
Bob Lehmann
Guest
Posts: n/a
 
      12-13-2007
http://databases.aspfaq.com/database...om-record.html

Bob Lehmann

"tamaker" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
> Is this do-able with ASP / VBscript?
>
> -- I have a database with user records (name, photo, etc. etc.)
>
> I want to use asp to generate (on the homepage) a series of 4 randomly
> selected 'user records' from the database -- say just the headshot
> photo or name from the database.
>
> In addition to the recordset being randomly generated (i.e. our of
> about 50 records, only records 4, 18, 23 and 26 are displayed) I need
> to MAKE SURE they are not duplicated (i.e. out of the 50 records,
> records 4, 18, 23 and 23 -- where the last two are duplicates).
>
> Is there a way I can pull this off and how do I go about it exactly?
>
>
> Thanks!
>



 
Reply With Quote
 
Dooza
Guest
Posts: n/a
 
      12-13-2007
tamaker wrote:
> Is this do-able with ASP / VBscript?
>
> -- I have a database with user records (name, photo, etc. etc.)
>
> I want to use asp to generate (on the homepage) a series of 4 randomly
> selected 'user records' from the database -- say just the headshot
> photo or name from the database.
>
> In addition to the recordset being randomly generated (i.e. our of
> about 50 records, only records 4, 18, 23 and 26 are displayed) I need
> to MAKE SURE they are not duplicated (i.e. out of the 50 records,
> records 4, 18, 23 and 23 -- where the last two are duplicates).
>
> Is there a way I can pull this off and how do I go about it exactly?


I have a stored procedure that does this, here is the SQL is uses:

SELECT TOP 5 item, desc, picture
FROM dbo.products
ORDER BY NEWID()

I haven't seen it create any duplicates yet.

Steve
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Database Database Database Database scott93727@gmail.com Computer Information 0 09-27-2012 02:43 AM
DataBase DataBase DataBase DataBase scott93727@gmail.com Computer Information 0 09-26-2012 09:40 AM
Select element from Set randomly? Davy C Programming 4 08-22-2005 08:03 PM
WIFI connection randomly/non randomly disconnects King Fu Wireless Networking 2 11-10-2004 07:03 AM
select of select box will select multiple in another box palmiere Javascript 1 02-09-2004 01:11 PM



Advertisments