2005
Retrieve Random Records using MySQL Query
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 retrieve random record,mysql random record,mysql sort random,mysql query random,mysql get random record,random query mysql,mysql random records,random record mysql,random query in mysql,MySQL sort by random,random mysql query,mysql query random record,mysql query random order,random sort mysql,mysql retrieve random records,mysql get random records,mysql random sort order,mysql random, 


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