Random selection

Discussion in 'ASP General' started by Jamie Fryatt, Nov 28, 2003.

  1. Jamie Fryatt

    Jamie Fryatt Guest

    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
     
    Jamie Fryatt, Nov 28, 2003
    #1
    1. Advertising

  2. 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" <> wrote in message news:bq7kql$64o$...
    > 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
    >
    >
     
    Graham Mattingley, Nov 28, 2003
    #2
    1. Advertising

  3. Jamie Fryatt

    PL Guest

    > 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.
     
    PL, Nov 28, 2003
    #3
  4. Jamie Fryatt

    MePadre Guest

    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" <> wrote in

    message news:bq7kql$64o$...
    >> 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
    >>
    >>
     
    MePadre, Nov 28, 2003
    #4
  5. Jamie Fryatt

    Jamie Fryatt Guest

    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" <> wrote in message
    news:...
    > > 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.
    >
    >
     
    Jamie Fryatt, Dec 1, 2003
    #5
  6. Jamie Fryatt

    Bob Barrows Guest

    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=...8&q=random select Access database&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.
     
    Bob Barrows, Dec 1, 2003
    #6
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Simon Niederberger
    Replies:
    2
    Views:
    16,616
    Christian Kaufhold
    Jan 7, 2005
  2. Andrew Crowe
    Replies:
    1
    Views:
    4,495
    Andrew Crowe
    Sep 13, 2004
  3. globalrev
    Replies:
    4
    Views:
    782
    Gabriel Genellina
    Apr 20, 2008
  4. curiousity

    HOWTO autopost the selection list upon selection

    curiousity, Nov 21, 2003, in forum: ASP .Net Mobile
    Replies:
    0
    Views:
    279
    curiousity
    Nov 21, 2003
  5. VK
    Replies:
    15
    Views:
    1,210
    Dr J R Stockton
    May 2, 2010
Loading...

Share This Page