Select random records from mySQL database

Discussion in 'PHP' started by Kyriakos, Sep 17, 2009.

  1. #1
    hi,

    how i can select some random records from my database?
     
    Kyriakos, Sep 17, 2009 IP
  2. stephan2307

    stephan2307 Well-Known Member

    Messages:
    1,277
    Likes Received:
    33
    Best Answers:
    7
    Trophy Points:
    150
    #2
    Simple

    
    SELECT * FROM table ORDER BY RAND();
    
    PHP:
     
    stephan2307, Sep 17, 2009 IP
  3. krishmk

    krishmk Well-Known Member

    Messages:
    1,376
    Likes Received:
    40
    Best Answers:
    0
    Trophy Points:
    185
    #3
    MySql's RAND() function might be too slow if you have huge number of records/rows.
    If you are fetching only 1 random record, probably you could seed a random number thru php and then fetch the specific row based on its ID.
     
    krishmk, Sep 17, 2009 IP
  4. creativeGenius

    creativeGenius Well-Known Member

    Messages:
    273
    Likes Received:
    5
    Best Answers:
    1
    Trophy Points:
    120
    #4
    your suggestion is good in theory, but greatly flawed, a random id is of course random, so how do you make sure the random number generated exists as an id?
     
    creativeGenius, Sep 17, 2009 IP
  5. pixmania

    pixmania Peon

    Messages:
    229
    Likes Received:
    3
    Best Answers:
    0
    Trophy Points:
    0
    #5
    Give it a rough between RAND(1,1000) ?
     
    pixmania, Sep 17, 2009 IP
  6. creativeGenius

    creativeGenius Well-Known Member

    Messages:
    273
    Likes Received:
    5
    Best Answers:
    1
    Trophy Points:
    120
    #6
    still no dice, what you can probably do is
    -query for the total number of records, or
    -query for the highest id

    which is still no foolproof since, what if one record gets deleted, eg: id = 10
    if you rand a number between the lowest possible eg 1,100, and you get 10
    then your script will break..
     
    creativeGenius, Sep 17, 2009 IP
  7. anthonywebs

    anthonywebs Banned

    Messages:
    657
    Likes Received:
    13
    Best Answers:
    0
    Trophy Points:
    0
    #7
    $id = "SELECT * FROM table ORDER BY id DESC LIMIT 1";

    to get the id of the last row then

    $rand = rand(0, $id);

    then

    SELECT * FROM table WHERE id = $id
     
    anthonywebs, Sep 17, 2009 IP
  8. creativeGenius

    creativeGenius Well-Known Member

    Messages:
    273
    Likes Received:
    5
    Best Answers:
    1
    Trophy Points:
    120
    #8
    hmm, i already mentioned this on my previous post, you run a risk of generating a value for an id that doesnt exist
     
    creativeGenius, Sep 17, 2009 IP
  9. krishmk

    krishmk Well-Known Member

    Messages:
    1,376
    Likes Received:
    40
    Best Answers:
    0
    Trophy Points:
    185
    #9
    If the query doesnt return any result for a particular id, you could try generating a new number.
    However if you have only a few hundreds of records/rows you could depend on the MySql RAND() function itself.
     
    krishmk, Sep 17, 2009 IP
  10. lawracri

    lawracri Peon

    Messages:
    74
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #10
    I think MySql RAND() function is good, how many records is considered huge? hundreds of millions?
     
    lawracri, Sep 17, 2009 IP
  11. pixmania

    pixmania Peon

    Messages:
    229
    Likes Received:
    3
    Best Answers:
    0
    Trophy Points:
    0
    #11
    Maybe just something as simple as this would do the trick,

    
    $info = $row['something']; // a column from the selected table 
    if (empty($info)){ // do we have any data from the something column ?
        $info = header( "Location: $ThisURL" ) ; // no data, then refresh the page
        }
    PHP:
    Of course this would only come into play if the rand() id number generated didn't exists, meaning its just a failsafe function that would refresh the page on the website visitor until sometime could be displayed. I doubt very much this would interfere with the visitors experience, its just a random event.
     
    pixmania, Sep 18, 2009 IP
  12. superscript

    superscript Peon

    Messages:
    29
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #12
    How can I update a random row in a table, and update one field after the row has been randomly selected?
     
    superscript, Nov 28, 2010 IP
  13. drctaccess

    drctaccess Peon

    Messages:
    62
    Likes Received:
    1
    Best Answers:
    1
    Trophy Points:
    0
    #13
    It is simple:


    ===============================================


    $result = mysql_query('SELECT some_id FROM some_table');
    if (!$result)
    {
    echo 'Could not run query: ' . mysql_error();
    exit;
    }
    $ids = array();
    $i = 0;
    while ($row = mysql_fetch_array($result, MYSQL_NUM))
    {
    $ids[$row[0]] = $i;
    $i++;
    }
    $rand_row = array_rand($ids, 1);
    $some_value = 'New content';
    $result = mysql_query('UPDATE some_table SET some_field=' . $some_value . ' WHERE some_id=' . $rand_row);
    echo 'Done';



    ===========================================

    replace some_table with your table_name
    replace some_id with your unique id for the row -> usually primary key
    replace some_field with your row field to be updated
    replace "New content" with your new value

    Enjoy :)
     
    drctaccess, Nov 29, 2010 IP