NLCJ Posted May 1, 2010 Share Posted May 1, 2010 Hello, I was trying to get a random value from the MySQL where one value is less than the other one. I've got this (so far). SELECT * FROM table WHERE status='1' AND value1<value2 ORDER BY rand() Value 1 is a variable from the same table as where it should get a random row from, the same counts for value2. I'm kinda stuck here... Regards, NLCJ Quote Link to comment https://forums.phpfreaks.com/topic/200383-problem-getting-information-out-of-a-table/ Share on other sites More sharing options...
andrewgauger Posted May 2, 2010 Share Posted May 2, 2010 Well what you quoted will compare value1 to value2 on the same row. ORDER BY rand() does not scale well. What you should to is $sql="SELECT count(*) as upper_limit FROM table WHERE status='1' AND value1<value2"; //todo: execute $sql... $result=mysql_fetch_assoc($sqlresult) $within_range=rand(1,$result['upper_limit']); $sql="SELECT * FROM table WHERE status='1' AND value1<value2 LIMIT $within_range , 1"; //todo execute $sql Quote Link to comment https://forums.phpfreaks.com/topic/200383-problem-getting-information-out-of-a-table/#findComment-1051787 Share on other sites More sharing options...
Ken2k7 Posted May 2, 2010 Share Posted May 2, 2010 SELECT * FROM table WHERE status='1' AND value1<value2 ORDER BY rand() LIMIT 1; Quote Link to comment https://forums.phpfreaks.com/topic/200383-problem-getting-information-out-of-a-table/#findComment-1051794 Share on other sites More sharing options...
NLCJ Posted May 2, 2010 Author Share Posted May 2, 2010 Thank you, it's working. Quote Link to comment https://forums.phpfreaks.com/topic/200383-problem-getting-information-out-of-a-table/#findComment-1051883 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.