PHP Learner Posted September 16, 2011 Share Posted September 16, 2011 Hello coders, I'm using this code to grab random results from a table and it works well but I need it to be a non-repeating event. $offset_result = mysql_query( " SELECT FLOOR(RAND() * COUNT(*)) AS `offset` FROM `jobs_board` "); $offset_row = mysql_fetch_object( $offset_result ); $offset = $offset_row->offset; I'm using this query followed by the html which is a set of divs with inline echo statements for the various variables I've localised from the table. Then I'm repeating that process a number of times to create a small list (I wanted to use a single while loop but cant figure it out at the moment). Anways, how can I make it so each query is random but also not repeated? At the moment they are indeed random but sometimes one same entry will appear a number of times consecutively... Nice one guys, L-plate Quote Link to comment Share on other sites More sharing options...
voip03 Posted September 16, 2011 Share Posted September 16, 2011 You only have to look at the total number of records in the table, and them do a rand() between 0 and the resulting number of records. So if you add more records, the rand() funtion will include them, and so on. // $Limit = total number of records ; SELECT * FROM table_name ORDER BY RAND() LIMIT $Limit ; Quote Link to comment Share on other sites More sharing options...
voip03 Posted September 16, 2011 Share Posted September 16, 2011 For your reading http://dev.mysql.com/doc/refman/5.5/en/mathematical-functions.html#function_rand Quote Link to comment Share on other sites More sharing options...
PHP Learner Posted September 16, 2011 Author Share Posted September 16, 2011 Thanks very much, but, the guy that wrote the above code saysabout the rand().. "The problem with this method is that it is very slow. The reason for it being so slow is that MySQL creates a temporary table with all the result rows and assigns each one of them a random sorting index." My table is gonna become massive, with thousands of entries so I chose this method to overcome snags in the future. Not sure if this is correct but others who commented on the page seem to agree. Is there anyway of adapting the code to counter repeating entries? Or something you can do with PHP? Cheers, pLate Quote Link to comment Share on other sites More sharing options...
voip03 Posted September 16, 2011 Share Posted September 16, 2011 You'll need to use some basic offset / limit with a random number type scheme. Quote Link to comment Share on other sites More sharing options...
PHP Learner Posted September 16, 2011 Author Share Posted September 16, 2011 Sounds great Voip mate!... erm.. How do you do that? Quote Link to comment Share on other sites More sharing options...
xyph Posted September 16, 2011 Share Posted September 16, 2011 If efficiency is key here, you're going to end up in a complex mess of a query, with possible sub-tables for sorting and triggers to keep it all organized http://jan.kneschke.de/projects/mysql/order-by-rand/ Quote Link to comment Share on other sites More sharing options...
xyph Posted September 16, 2011 Share Posted September 16, 2011 Here's another good read that is a little slower, but offers solutions fast enough for your implementations http://www.warpconduit.net/2011/03/23/selecting-a-random-record-using-mysql-benchmark-results/ Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.