2005
Retrieve Random Records using MySQL Query
Sponsored Links
This is probably an old trick. But I still see some programmer, instead of directly using query, they:
- grab the whole records first
- put them in an array
- generate random number, for example using PHP
rand()ormk_rand() - finally get the record from the array using the random number generated as the index.
If we only need to retrieve one random record from the database, without any further rule, I think those are too much. We can simplify the process by using MySQL query like below:
select * from [table_name] where [additional_condition] order by rand() limit [number_of_record_to_retrieve]
Using MySQL build-in rand() function, we can force the query to sort the result in random order. So if we grab the first n record(s), the return is always different.
Retrieve Random Records using MySQL Query is written by cosa and posted under Programming , mysql, php. If you like it, you might consider subscribing to our feed, follows us on Twitter, or receive our latest posts via email. Or else, you could also or store it to your favourite social bookmark sites. Further information about this article can be found.
And while you're here, why don't you check out our other articles:
Pssst! Most people are coming to this page searching for: mysql random query,mysql random sort,mysql query random,mysql random record,mysql sort random,mysql retrieve random record,mysql get random record,random query mysql,random record mysql,MySQL sort by random,mysql random records,random mysql query,mysql query random record,random query in mysql,mysql query random order,random sort mysql,mysql random,query random,random record from mysql,mysql retrieve random records, 


thanks it works perfect
This is the result of a random query. I was at id=237.
So tricky (=
thx . it’s helpful .