SQL PHP HTML ASP JavaScript articles and free scripts to download

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.
Related Tutorial
limit query
Order by Query

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

To get 10 random records from the same table.

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

Let us learn how to randomly update records of a table in Part II

Google+

Dev Pandey12-04-2012
select * from tableName order by rand() limit 0,1;
alex11-04-2013
What is the use of Random records ?
Post Comment This is for short comments only. Use the forum for more discussions.
Name
Email( not to be displayed)Privacy Policy
1+2=This is to prevent automatic submission by spammers. Please enter the result of the sum as asked



Join Our Email List
Email:  
For Email Newsletters you can trust

HTML . MySQL. PHP. JavaScript. ASP. Photoshop. Articles. FORUM Contact us

©2000-2014 plus2net.com All rights reserved worldwide Privacy Policy Disclaimer