469,352 Members | 1,697 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,352 developers. It's quick & easy.

Random selection

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
Jul 19 '05 #1
5 4932
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

"Jamie Fryatt" <ja**********@btconnect.com> wrote in message news:bq**********@titan.btinternet.com...
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

Jul 19 '05 #2
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.
Jul 19 '05 #3
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" <ja**********@btconnect.com> wrote in

message news:bq**********@titan.btinternet.com...
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

Jul 19 '05 #4
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" <pb****@yahoo.se> wrote in message
news:OT**************@TK2MSFTNGP09.phx.gbl...
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.

Jul 19 '05 #5
Jamie Fryatt wrote:
i am using a standard ms access database!

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

http://groups.google.com/groups?hl=e...se&sa=N&tab=wg

HTH,
Bob Barrows
--
Microsoft MVP -- ASP/ASP.NET
Please reply to the newsgroup. The email account listed in my From
header is my spam trap, so I don't check it very often. You will get a
quicker response by posting to the newsgroup.
Jul 19 '05 #6

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

4 posts views Thread by Keith Griffiths | last post: by
3 posts views Thread by Gunnar | last post: by
2 posts views Thread by IceCube | last post: by
2 posts views Thread by Tartifola | last post: by
13 posts views Thread by Bruza | last post: by
3 posts views Thread by Manuel Ebert | last post: by
1 post views Thread by CARIGAR | last post: by
reply views Thread by zhoujie | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.