Return to Snippet

Revision: 13339
at April 20, 2009 11:31 by BFTrick


Initial Code
SELECT MAX(id) FROM table

<?php
  $random_num = rand(0, $max(id));
?>

SELECT * FROM table WHERE id >= $random_num LIMIT 1

Initial URL


Initial Description
This code is useful if you have a table where some records have been deleted.  This code will favor records after large blocks of deleted records.

1) Get max id
2) generate random number with the max id
3) Query DB with random number.

Initial Title
Get random record in table

Initial Tags


Initial Language
MySQL