abdfahim Posted January 7, 2008 Share Posted January 7, 2008 the following code SELECT * FROM `table1 WHERE `col1` BETWEEN a AND b will work only if a>b. But I don't know whether a or b is greater, but I always want to return the values between a and b. what should i do? Quote Link to comment Share on other sites More sharing options...
Cazrin Posted January 7, 2008 Share Posted January 7, 2008 I guess you could put an if() statement before that, to check which is greater. <?php $a = 400; $b = 300 if($a>$b) { // Swap the values of $a and $b $temp = $b; $b=$a; // Is now 400 $a=$temp; // Is now 300 } $query = "SELECT * FROM `table1` WHERE `col1` BETWEEN ".$a." and ".$b; ?> Haven't used BETWEEN before so not sure if that would work But I'd say give it a go, makes sense to me! Quote Link to comment Share on other sites More sharing options...
abdfahim Posted January 7, 2008 Author Share Posted January 7, 2008 yup, that's a nice solution. But isn't there any built in function in SQL which will solve this?? may be not !! Quote Link to comment Share on other sites More sharing options...
fenway Posted January 7, 2008 Share Posted January 7, 2008 What do you mean by "you don't know"... is this value in the db or not? Quote Link to comment Share on other sites More sharing options...
abdfahim Posted January 10, 2008 Author Share Posted January 10, 2008 no, both a and b is the input given by the user in two text boxes. I distinguish the two text boxes as higher and lower, but if some one just give inverse entry, then it will show error. Quote Link to comment Share on other sites More sharing options...
fenway Posted January 10, 2008 Share Posted January 10, 2008 no, both a and b is the input given by the user in two text boxes. I distinguish the two text boxes as higher and lower, but if some one just give inverse entry, then it will show error. You could get mysql to determine this for you, but that's much more work when any type of validation would prevent this issue on the server side. 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.