Grabbing 5 random and unique rows from a database
Posted: Mon Nov 17, 2003 12:32 am
Hello,
I want to grab 5 randomly selected rows from a table and output it in HTML. I wrote a script that counted the number of rows in a table (somewhere around 200 of them). Then, I used the rand() function to generate random numbers between 0 and the number of rows in the table, so I could then pick a random field and output it. However, I am relying on the "id" field, because it is numerically indexed. There are a good 20 numbers that are missing because they have been deleted. This means that my results sometimes show blanks, because the randomly selected field does not exist. Any ideas on how I can only select tables randomly that actually exist? Knowing this, can I select 5 unique ones so I don't have any duplicates?[/mysql_man]
I want to grab 5 randomly selected rows from a table and output it in HTML. I wrote a script that counted the number of rows in a table (somewhere around 200 of them). Then, I used the rand() function to generate random numbers between 0 and the number of rows in the table, so I could then pick a random field and output it. However, I am relying on the "id" field, because it is numerically indexed. There are a good 20 numbers that are missing because they have been deleted. This means that my results sometimes show blanks, because the randomly selected field does not exist. Any ideas on how I can only select tables randomly that actually exist? Knowing this, can I select 5 unique ones so I don't have any duplicates?[/mysql_man]