Random Record
Question posted by: Bill
(Guest)
on
November 13th, 2005 12:59 AM
Hello,
I am trying to use a SQL Query to return a random record from an
Access 2000 Database.
I am using:
SELECT TOP 1 Example
FROM TABLE
ORDER BY Rnd[(ID)];
But, I am not getting a random record, I get the same record returned
each time. If I create a SQL query in Access, and use the order by
rnd, I will get a random record.
Does anyone know how to return a random record with a SQL query?
Thank you for the help,
Bill Kramer
7
Answers Posted
I have no idea on how this works, but I did find a solution (I think)
Try:
Sorry, previous post left me too fast...
Here is the syntax (x = table, ID = field)
SELECT TOP 1 x.ID
FROM x
ORDER BY Rnd([id])*[id]*Now();
This works for me
Thank you very much for your reply. Unfortunately I wasn't able to get
your code to work for me. I haven't found a solution yet.
But thank you very much for your effort.
-Bill Kramer
"WindAndWaves" <access@ngaru.com> wrote in message news:<P42Bc.1520$LT3.57426@news.xtra.co.nz>...[color=blue]
> Sorry, previous post left me too fast...
>
> Here is the syntax (x = table, ID = field)
>
>
> SELECT TOP 1 x.ID
> FROM x
> ORDER BY Rnd([id])*[id]*Now();
>
> This works for me[/color]
This works, but it always pulls the same data! I guess the starting
SEED is not accurate?
WindAndWaves wrote:[color=blue]
> Sorry, previous post left me too fast...
>
> Here is the syntax (x = table, ID = field)
>
>
> SELECT TOP 1 x.ID
> FROM x
> ORDER BY Rnd([id])*[id]*Now();
>
> This works for me[/color]
This works, but it always pulls the same data! I guess the starting
SEED is not accurate?
WindAndWaves wrote:[color=blue]
> Sorry, previous post left me too fast...
>
> Here is the syntax (x = table, ID = field)
>
>
> SELECT TOP 1 x.ID
> FROM x
> ORDER BY Rnd([id])*[id]*Now();
>
> This works for me[/color]
You need to use the Randomize method first. Check the Help file.
--
PC Datasheet
Your Resource For Help With Access, Excel And Word Applications
Join Bytes!
www.pcdatasheet.com
"gmsmstr" <dmburgess@surdyke.com> wrote in message
news:1106949893.104979.158630@f14g2000cwb.googlegr oups.com...[color=blue]
> This works, but it always pulls the same data! I guess the starting
> SEED is not accurate?
>
> WindAndWaves wrote:[color=green]
> > Sorry, previous post left me too fast...
> >
> > Here is the syntax (x = table, ID = field)
> >
> >
> > SELECT TOP 1 x.ID
> > FROM x
> > ORDER BY Rnd([id])*[id]*Now();
> >
> > This works for me[/color]
>[/color]
The ORDER BY clause does not affect which records are chosen, only the order
in which the records that have been selected are presented. You would need a
WHERE or HAVING clause to affect the selection.
Just for the record, RANDOMIZEing prior to executing this SQL will have no
effect on the records returned.
Larry Linson
Microsoft Access MVP
"gmsmstr" <dmburgess@surdyke.com> wrote in message
news:1106949893.104979.158630@f14g2000cwb.googlegr oups.com...[color=blue]
> This works, but it always pulls the same data! I guess the starting
> SEED is not accurate?
>
> WindAndWaves wrote:[color=green]
> > Sorry, previous post left me too fast...
> >
> > Here is the syntax (x = table, ID = field)
> >
> >
> > SELECT TOP 1 x.ID
> > FROM x
> > ORDER BY Rnd([id])*[id]*Now();
> >
> > This works for me[/color]
>[/color]
|
|
|
What is Bytes?
We are a network of experts and professionals in IT and software development that help one another with answers to tough questions and share insights.
Get the best answers to your questions from over 196,901 network members.
Top Community Contributors
|