Results 1 to 8 of 8

Thread: random record

  1. #1

    Thread Starter
    Addicted Member
    Join Date
    Oct 2012
    Posts
    193

    random record

    good day.

    I have this table with id number and fullname.
    Let say the table have 100,000 records.
    I want to create a query that will show randomized select 20 records out of 100,000.

    Can anybody show me how? I already researched on google but got no result. Thanks in advance.

  2. #2
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    110,350

    Re: random record

    The best way to do many things depends on the database, which you haven't told us.

  3. #3

    Thread Starter
    Addicted Member
    Join Date
    Oct 2012
    Posts
    193

    Re: random record

    oh, i forgot, im using mysql.

    just now i saw a query but they said it will be slow if it involves million records

    SELECT col1 FROM tbl WHERE RAND() limit 100;

  4. #4
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    110,350

    Re: random record

    Your table doesn't contain millions of records so no problem.

  5. #5

    Thread Starter
    Addicted Member
    Join Date
    Oct 2012
    Posts
    193

    Re: random record

    Quote Originally Posted by jmcilhinney View Post
    Your table doesn't contain millions of records so no problem.
    that could be but supposing it has those number of records

  6. #6
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    110,350

    Re: random record

    Then the query will be slow.

  7. #7
    Frenzied Member
    Join Date
    Jun 2014
    Posts
    1,084

    Re: random record

    just a general idea how to select 20 random records out of 100000 or 2000000 or whatever at comparable speed:

    do until there are 20 numbers stored
    generate a random number between the lowest ID and the highest ID
    if the generated number is an ID in the table, and not yet stored then store it
    loop

    now you have 20 distinct record id's
    do not put off till tomorrow what you can put off forever

  8. #8
    PowerPoster ChrisE's Avatar
    Join Date
    Jun 2017
    Location
    Frankfurt
    Posts
    3,048

    Re: random record

    Quote Originally Posted by genlight View Post
    oh, i forgot, im using mysql.

    just now i saw a query but they said it will be slow if it involves million records

    SELECT col1 FROM tbl WHERE RAND() limit 100;
    Hi,
    try it this way...
    Code:
    $x = 20;
    $sql = "SELECT Field1,
                   Field2,
                   Field3
            FROM table1
            ORDER BY RAND()
            LIMIT ".$x;
    $query = mysql_query($sql);

    this would be for Access...
    Code:
    SELECT TOP 20 Orders.OrderID, Orders.CustomerID
    FROM Orders
    ORDER BY rnd(ISNULL(Orders.OrderID) * 0 +1);
    regards
    Chris
    to hunt a species to extinction is not logical !
    since 2010 the number of Tigers are rising again in 2016 - 3900 were counted. with Baby Callas it's 3901, my wife and I had 2-3 months the privilege of raising a Baby Tiger.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width