Jump to content

Recommended Posts

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

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

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.