RE: [PHP] querying for values inside range
| From: | Jon Haworth | Date: | Tue, 18 Mar 2003 11:16:21 +0000 |
| Subject: | RE: [PHP] querying for values inside range | ||
| Groups: | php.general | ||
| Request: | Send a blank email to php-general+get-140106@lists.php.net to get a copy of this message | ||
Hi Jason,
> $sql = "select * from table where age>=$age1 and age<=$age2";
> what if i don't know which is value is greater, age1 or age2?
PHP has a couple of handy functions for this: min() and max()
$min = min($age1, $age2); // get smallest of two values
$max = max($age1, $age2); // get largest of two values
$sql = "select * from table where age >= $min and age <= $max";
You could also use BETWEEN in your query, which I think reads better:
$sql = "select * from table where age between $min and $max";
This isn't connected to your question, but have a look at this when you have
a spare 5 minutes:
http://adopenstatic.com/faq/selectstarisbad.asp
Cheers
Jon