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

TOP

Do you find it easy to navigate through this site?

 Yes

 No

 Where am I?