SQL PHP HTML ASP JavaScript articles and free scripts to download If you are facing any problem in viewing this page, please tell us
 

SQL for collecting random record from a table


Many times we have to collect a random record from mysql database. How to get the random record?
Here we have to use rand() function and along with it we will use limit query to restrict our result to one record. Randome funciton is used along with order by clause to generate randome records. This will work well in other database also. Here is the query. We have added on where clause also.


SELECT * FROM $table_name where status='yes' ORDER BY RAND() limit 1

To get 10 random records from the same table.

SELECT * FROM $table_name where status='yes' ORDER BY RAND() limit 10




Further readings
Getting second highest number from the student table



Sections
PHP
JavaScript
ASP
HTML
SQL
Photoshop
Articles SEO
SQL Tutorial List
SQL Commands
SQL Sections
Date & Time
Join Table
String
Math
Subscribe
Submit your email address and receive article and product notifications. Your email is safe with us.