I did some research and I found this on
http://www.greggdev.com/web/articles.php?id=6As of this writing, there is no automatic way for MySQL to select a random row from a database table. Extracting a random row from a table can be useful for many reasons. It can pull up random products, random advertisments, or any other random thing that you have stored in your database table.
For most purposes on smaller database tables, the following will work fine:
$random_row = mysql_fetch_row(mysql_query("select * from YOUR_TABLE order by rand() limit 1"));
$random_row will be an array containing the data extracted from the random row. However, when the table is large (over about 10,000 rows) this method of selecting a random row becomes increasingly slow with the size of the table and can create a great load on the server. I tested this on a table I was working that contained 2,394,968 rows. It took 717 seconds (12 minutes!) to return a random row.
I wrote the script below as a workaround for a random row with a large database table. This will return a random row in about 0.05 seconds, regardless of the size of the table. If the max_id of the table is not dynamically changing, the function can be rewritten to only execute one database query instead of two.
I am using this script for a new search engine spidering project I am working on. With this, it is useful to have an tinyint(1) column in the table that is updated as the row is selected so that it will not be chosen again. Since the function uses greater than and less than, it would not return an empty result unless all rows had been updated at which point the script could be ended. For such a script this also allows many updaters to be running at once selecting random rows that will not be selected again because the tinyint will be changed upon selection.
<?php
//CODE FROM
WWW.GREGGDEV.COM function random_row($table, $column) {
$max_sql = "SELECT max(" . $column . ")
AS max_id
FROM " . $table;
$max_row = mysql_fetch_array(mysql_query($max_sql));
$random_number = mt_rand(1, $max_row['max_id']);
$random_sql = "SELECT * FROM " . $table . "
WHERE " . $column . " >= " . $random_number . "
ORDER BY " . $column . " ASC
LIMIT 1";
$random_row = mysql_fetch_row(mysql_query($random_sql));
if (!is_array($random_row)) {
$random_sql = "SELECT * FROM " . $table . "
WHERE " . $column . " < " . $random_number . "
ORDER BY " . $column . " DESC
LIMIT 1";
$random_row = mysql_fetch_row(mysql_query($random_sql));
}
return $random_row;
}
//USAGE
echo '<pre>';
print_r(random_row('YOUR_TABLE', 'YOUR_COLUMN'));
echo '</pre>';
?>
ThAs of this writing, there is no automatic way for MySQL to select a random row from a database table. Extracting a random row from a table can be useful for many reasons. It can pull up random products, random advertisments, or any other random thing that you have stored in your database table.
For most purposes on smaller database tables, the following will work fine:
$random_row = mysql_fetch_row(mysql_query("select * from YOUR_TABLE order by rand() limit 1"));
$random_row will be an array containing the data extracted from the random row. However, when the table is large (over about 10,000 rows) this method of selecting a random row becomes increasingly slow with the size of the table and can create a great load on the server. I tested this on a table I was working that contained 2,394,968 rows. It took 717 seconds (12 minutes!) to return a random row.
I wrote the script below as a workaround for a random row with a large database table. This will return a random row in about 0.05 seconds, regardless of the size of the table. If the max_id of the table is not dynamically changing, the function can be rewritten to only execute one database query instead of two.
I am using this script for a new search engine spidering project I am working on. With this, it is useful to have an tinyint(1) column in the table that is updated as the row is selected so that it will not be chosen again. Since the function uses greater than and less than, it would not return an empty result unless all rows had been updated at which point the script could be ended. For such a script this also allows many updaters to be running at once selecting random rows that will not be selected again because the tinyint will be changed upon selection.
<?php
//CODE FROM
WWW.GREGGDEV.COM function random_row($table, $column) {
$max_sql = "SELECT max(" . $column . ")
AS max_id
FROM " . $table;
$max_row = mysql_fetch_array(mysql_query($max_sql));
$random_number = mt_rand(1, $max_row['max_id']);
$random_sql = "SELECT * FROM " . $table . "
WHERE " . $column . " >= " . $random_number . "
ORDER BY " . $column . " ASC
LIMIT 1";
$random_row = mysql_fetch_row(mysql_query($random_sql));
if (!is_array($random_row)) {
$random_sql = "SELECT * FROM " . $table . "
WHERE " . $column . " < " . $random_number . "
ORDER BY " . $column . " DESC
LIMIT 1";
$random_row = mysql_fetch_row(mysql_query($random_sql));
}
return $random_row;
}
//USAGE
echo '<pre>';
print_r(random_row('YOUR_TABLE', 'YOUR_COLUMN'));
echo '</pre>';
?>
As of this writing, there is no automatic way for MySQL to select a random row from a database table. Extracting a random row from a table can be useful for many reasons. It can pull up random products, random advertisments, or any other random thing that you have stored in your database table.
For most purposes on smaller database tables, the following will work fine:
$random_row = mysql_fetch_row(mysql_query("select * from YOUR_TABLE order by rand() limit 1"));
$random_row will be an array containing the data extracted from the random row. However, when the table is large (over about 10,000 rows) this method of selecting a random row becomes increasingly slow with the size of the table and can create a great load on the server. I tested this on a table I was working that contained 2,394,968 rows. It took 717 seconds (12 minutes!) to return a random row.
I wrote the script below as a workaround for a random row with a large database table. This will return a random row in about 0.05 seconds, regardless of the size of the table. If the max_id of the table is not dynamically changing, the function can be rewritten to only execute one database query instead of two.
I am using this script for a new search engine spidering project I am working on. With this, it is useful to have an tinyint(1) column in the table that is updated as the row is selected so that it will not be chosen again. Since the function uses greater than and less than, it would not return an empty result unless all rows had been updated at which point the script could be ended. For such a script this also allows many updaters to be running at once selecting random rows that will not be selected again because the tinyint will be changed upon selection.
<?php
//CODE FROM
WWW.GREGGDEV.COM function random_row($table, $column) {
$max_sql = "SELECT max(" . $column . ")
AS max_id
FROM " . $table;
$max_row = mysql_fetch_array(mysql_query($max_sql));
$random_number = mt_rand(1, $max_row['max_id']);
$random_sql = "SELECT * FROM " . $table . "
WHERE " . $column . " >= " . $random_number . "
ORDER BY " . $column . " ASC
LIMIT 1";
$random_row = mysql_fetch_row(mysql_query($random_sql));
if (!is_array($random_row)) {
$random_sql = "SELECT * FROM " . $table . "
WHERE " . $column . " < " . $random_number . "
ORDER BY " . $column . " DESC
LIMIT 1";
$random_row = mysql_fetch_row(mysql_query($random_sql));
}
return $random_row;
}
//USAGE
echo '<pre>';
print_r(random_row('YOUR_TABLE', 'YOUR_COLUMN'));
echo '</pre>';
?>
Thanks to everyone for responding. The article above gives a solution from a PHP viewpoint (which is perfect since I am working with PHP) though the script above may be complex.