By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
435,157 Members | 891 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 435,157 IT Pros & Developers. It's quick & easy.

Random selection

P: n/a
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
Share this Question
Share on Google+
5 Replies


P: n/a
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

P: n/a
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

P: n/a
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

P: n/a
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

P: n/a
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.