krispykreme Posted February 14, 2008 Share Posted February 14, 2008 random selection of one row is what i want right now no filtering needed, but in the future i want to be able to randomly select from a user that has been offline for 24 hours givin a time contraint what would be the best way to do this? ive never done a random selection in a query so i want to be guided to the best way from the start Quote Link to comment Share on other sites More sharing options...
pocobueno1388 Posted February 15, 2008 Share Posted February 15, 2008 Here is an example of selecting one random row SELECT * FROM table ORDER BY RAND() LIMIT 1 Quote Link to comment Share on other sites More sharing options...
krispykreme Posted February 15, 2008 Author Share Posted February 15, 2008 this method seems to look at all of the rows? id select_type table type possible_keys key key_len ref rows Extra 1 SIMPLE users ALL NULL NULL NULL NULL 70064 Using temporary; Using filesort Quote Link to comment Share on other sites More sharing options...
pocobueno1388 Posted February 15, 2008 Share Posted February 15, 2008 It only returns one row, correct? You can replace the '*' with whatever columns you need. I'm not sure if thats what you were talking about though. Quote Link to comment Share on other sites More sharing options...
krispykreme Posted February 15, 2008 Author Share Posted February 15, 2008 na i mean it goes through 70,000 rows just to get that one row it isnt a huge issue, but if somehow this became 7,000,000 rows it would possibly be and i didnt know if there was a more optimized way = / Quote Link to comment Share on other sites More sharing options...
powerspike Posted February 15, 2008 Share Posted February 15, 2008 a hackisk way to get it done, would be to get the amount rows in the table, like "select row_id from table where order by row_id DESC limit 0,1"; then do a "select * from table where row_id = " . rand(0,$row_id) . "' limit 0,1"; ($row_id would be the row_id from the first query). As i said, it's a hackish way, i'm quite sure there is a better way to do it thou. PS, this assumes you have a row_id and it's in some type of order. Quote Link to comment Share on other sites More sharing options...
AndyB Posted February 15, 2008 Share Posted February 15, 2008 read the 'sticky' at the top of this forum!! http://www.phpfreaks.com/forums/index.php/topic,125759.0.html 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.