MySQL Query to Select Random Records

Often in many situations during programming you might need to pull randomly selected records from a MySQL Database. For example displaying 5 randomly selected ads in your site. All that is needed is use rand() function.

SELECT ID FROM
ORDER BY RAND() LIMIT 5

Make sure you use the LIMIT clause as otherwise it would cause an overhead in the results. Note that if you use rand() function in a database with large number of records, the query would slowdown. So make sure you filter the necessary records and then apply rand() to pull out randomly selected records from the database.

Similar Posts:

Tags:

Balakrishnan Prabhu

Mr. Balakrishnan Prabhu is the founder of Corpocrat magazine. He is also the founder of Best Citizenships (BC), assisting wealthy individuals with with global citizenship and residency programs in Europe. His other interests are Linux, Machine learning, Wordpress, etc. You can contact him here