cyberkiller Posted August 22, 2007 Share Posted August 22, 2007 Is their a more efficient way to do this query? $query="SELECT domain_id,domain FROM domain ORDER BY rand() Limit 1"; Expected output is one random domain_id, domain from the table. Now I did read the sticky, http://www.phpfreaks.com/forums/index.php/topic,125759.0.html However to me the queries in that post looked more strainful than my current one. Link to comment https://forums.phpfreaks.com/topic/66165-order-by-rand-is-there-a-better-way/ Share on other sites More sharing options...
Illusion Posted August 22, 2007 Share Posted August 22, 2007 $query="SELECT domain_id,domain FROM domain ORDER BY rand(now()+1) Limit 1; Link to comment https://forums.phpfreaks.com/topic/66165-order-by-rand-is-there-a-better-way/#findComment-331046 Share on other sites More sharing options...
fenway Posted August 23, 2007 Share Posted August 23, 2007 $query="SELECT domain_id,domain FROM domain ORDER BY rand(now()+1) Limit 1; Ignore this... "more strainful"? Link to comment https://forums.phpfreaks.com/topic/66165-order-by-rand-is-there-a-better-way/#findComment-331551 Share on other sites More sharing options...
XaeroDegreaz Posted August 23, 2007 Share Posted August 23, 2007 Well there is a more 'random' way of doing it. However it requires 2 queries. One query to get the amount of results: $query = "SELECT * FROM `users` WHERE `blah` = 'bleh'" $max = mysql_num_rows($query) //This gives us our maximum value srand((double)microtime() * 1000000); $final = rand(0,$max); Then you do the query again (sorry, I know there is another way, but I can't remember how to recall that query) $query = "SELECT * FROM `users` WHERE `blah` = 'bleh' LIMIT $final,1" (or is it 1,$final) =}~ That will select a random row. That's actually what I've been using, because the rand() for mysql is totally out of line Good luck. Link to comment https://forums.phpfreaks.com/topic/66165-order-by-rand-is-there-a-better-way/#findComment-331685 Share on other sites More sharing options...
NArc0t1c Posted August 23, 2007 Share Posted August 23, 2007 uhm.. I have a theory that might say this could work aswell, <?php $Rows = mysql_num_rows(mysql_query("SELECT domain_id FROM domain")); $Random = rand(0,$Rows); $Query = "SELECT domain_id, domain FROM domain WHERE domain_id='" . $Random . "'"; ?> Link to comment https://forums.phpfreaks.com/topic/66165-order-by-rand-is-there-a-better-way/#findComment-331688 Share on other sites More sharing options...
Illusion Posted August 23, 2007 Share Posted August 23, 2007 $query="SELECT domain_id,domain FROM domain ORDER BY rand(now()+1) Limit 1; Ignore this... "more strainful"? why? Link to comment https://forums.phpfreaks.com/topic/66165-order-by-rand-is-there-a-better-way/#findComment-331699 Share on other sites More sharing options...
fenway Posted August 23, 2007 Share Posted August 23, 2007 Well there is a more 'random' way of doing it. However it requires 2 queries. One query to get the amount of results: $query = "SELECT * FROM `users` WHERE `blah` = 'bleh'" $max = mysql_num_rows($query) //This gives us our maximum value srand((double)microtime() * 1000000); $final = rand(0,$max); Then you do the query again (sorry, I know there is another way, but I can't remember how to recall that query) $query = "SELECT * FROM `users` WHERE `blah` = 'bleh' LIMIT $final,1" (or is it 1,$final) =}~ That will select a random row. That's actually what I've been using, because the rand() for mysql is totally out of line Good luck. That will be insanely slow if $final is large. uhm.. I have a theory that might say this could work aswell, <?php $Rows = mysql_num_rows(mysql_query("SELECT domain_id FROM domain")); $Random = rand(0,$Rows); $Query = "SELECT domain_id, domain FROM domain WHERE domain_id='" . $Random . "'"; ?> That's only useful if you want to do something very simple, without any "true" order by clause. $query="SELECT domain_id,domain FROM domain ORDER BY rand(now()+1) Limit 1; Ignore this... "more strainful"? why? Because it's still running RAND() for each row, which is what makes it slow -- having a seed makes no difference whatsoever. Link to comment https://forums.phpfreaks.com/topic/66165-order-by-rand-is-there-a-better-way/#findComment-332060 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.