dreamwest Posted February 23, 2010 Share Posted February 23, 2010 I had to remove rand from my queries because my database is over 3 million rows and it was serioulsly affecting the server Is it possible to compress this alternate rand query into one line aka one mysql_query(): $offset_result = mysql_query( " SELECT FLOOR(RAND() * COUNT(*)) AS `offset` FROM `video` "); $offset_row = mysql_fetch_object( $offset_result ); $offset = $offset_row->offset; $result3 = mysql_query( " SELECT * FROM `video` WHERE approve='1' and `show`='1' LIMIT $offset, 10 " ); Quote Link to comment https://forums.phpfreaks.com/topic/193016-removing-rand/ Share on other sites More sharing options...
sader Posted February 23, 2010 Share Posted February 23, 2010 hmm maybe let php do random stuff? $offset_result = mysql_query("SELECT id FROM `video`"); $offset = floor((rand() / getrandmax()) * mysql_num_rows($offset_result)); Quote Link to comment https://forums.phpfreaks.com/topic/193016-removing-rand/#findComment-1016515 Share on other sites More sharing options...
sader Posted February 23, 2010 Share Posted February 23, 2010 I just understand now one thing u should substract from rows count the number of items u want to display like so: $offset_result = mysql_query("SELECT id FROM `video`"); $offset = floor((rand() / getrandmax()) * (mysql_num_rows($offset_result)-10)); //just to be sure that u will always be able to retrive 10 videos from offset Quote Link to comment https://forums.phpfreaks.com/topic/193016-removing-rand/#findComment-1016524 Share on other sites More sharing options...
dreamwest Posted February 23, 2010 Author Share Posted February 23, 2010 Thanks. Problem is it doesnt reduce the amount of code for the query/ies and ill still have 3 queries. I was trying to reduce it to one combined query thus creating less code, easier to remember & less cluttered apps - it may be possible with subqueries, ill have to brush up on that Quote Link to comment https://forums.phpfreaks.com/topic/193016-removing-rand/#findComment-1016561 Share on other sites More sharing options...
fenway Posted February 24, 2010 Share Posted February 24, 2010 Unfortunately, you still can't use an expression in the limit clause... Quote Link to comment https://forums.phpfreaks.com/topic/193016-removing-rand/#findComment-1017474 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.