Learning Journal – 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:
// 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()”;
?>
via Learning Journal – Random Rows in MySQL.
Short and helpful.

