Random selection

J

Jamie Fryatt

Hi all, im a little bit new to all this so i you could help me a little i
would be greatful.

How do i select a completly random record from a database?

sql = select (random) from tbl, sort of thing.

Thanks for any help you can give

Jamie
 
G

Graham Mattingley

Hi Jamie,

is it ASP ???

you could do something like

select count(*) from tbl, sort of thing

randomize
count = {count varable from select (*)}
random_number=int(rnd*count)+1

select from tbl where id = random_number

something like this will do it

Graham
 
P

PL

How do i select a completly random record from a database?

The question is what kind of database ? Some databases actually
have a way of doing it directly, if it's SQL server you need to fool
it a bit and the code is not that efficent:

"SELECT TOP 1 [fieldname] from [yourtable] order by newid()"

PL.
 
M

MePadre

Forgive me if I am wrong, but does your suggestion not
require that all the IDs be sequential. If, for instance
you started with 200 records, and deleted every other one,
you would be left with 100 even numbered records. Using
your logic, the count would be 100 and the random_number
might be odd (i.e. 33). Since there is no record ID 33
(remember all odd records were deleted), the query would
return nothing.

Hi Jamie,>select count(*) from tbl, sort of thing

randomize
count = {count varable from select (*)}
random_number=int(rnd*count)+1

select from tbl where id = random_number

-----Original Message-----
is it ASP ???

you could do something like
something like this will do it

Graham





"Jamie Fryatt" <[email protected]> wrote in
message news:[email protected]...
 
J

Jamie Fryatt

i am using a standard ms access database!

i just need a select statement for a random record select if there is one?

Thanks

Jamie


PL said:
How do i select a completly random record from a database?

The question is what kind of database ? Some databases actually
have a way of doing it directly, if it's SQL server you need to fool
it a bit and the code is not that efficent:

"SELECT TOP 1 [fieldname] from [yourtable] order by newid()"

PL.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,055
Latest member
SlimSparkKetoACVReview

Latest Threads

Top