Your Ad Here

   
  Welcome to online books
  Random Rows in MySQL
 

Random Rows in MySQL

I have to admit that I had quite some trouble figuring out how I could get MySQL to return some random rows in a query. It was something I had to figure out for myself when I wanted to get a list of random websites off GIDTopsites™ to place on these pages here.

As usual, everything fell nicely into place and it was SO simple...

RAND()

Using rand(), I just had to add it to my SQL query like this:

php:


<?php
// random row mysql query example
$sql = "SELECT * FROM tablename
WHERE somefield='something'
ORDER BY RAND() LIMIT 5"
;

// or, something like this
$sql = "SELECT * FROM tablename
ORDER BY RAND()"
;
?>


That's it, that's all you have to do to get MySQL to return a set of random rows for you to do what you will with it

 
  Today, there have been 3 visitors (4 hits) on this page!  
 
This website was created for free with Own-Free-Website.com. Would you also like to have your own website?
Sign up for free