RE: [PHP] Get the lowest value out of different fields
| From: | Jennifer Goodie | Date: | Wed, 03 Sep 2003 19:53:52 +0000 |
| Subject: | RE: [PHP] Get the lowest value out of different fields | ||
| References: | 1 | Groups: | php.general |
| Request: | Send a blank email to php-general+get-161587@lists.php.net to get a copy of this message | ||
> Hi All,
>
> I hope you can help me with this:
>
> I have a tabel in my database like this
>
> TEST
>
> fieldname Value's
>
> testid 1
> testf1 3
> testf2 4
> testf3 0
> testf4 2
> testf5 0
>
> (so this is one record!)
>
> I want to display the lowest value, except 0.. So the SQL
> statement will be SELECT testf1, testf2, testf3, testf4, testf5
> FROM test where testid='1' .
>
> I can't figure out if i can do this into a SQL statement or put
> it in an Array and sort it..
>
> Please help!
>
If you really want to do this in an SQL statement and you're using mySQL you
can nest IFs, but it's really ugly. For example...
SELECT IF(IF(IF(IF(testf1< testf2 || testf2 = 0, testf1,testf2) < testf3 ||
testf3 = 0,IF(testf1< testf2, testf1,testf2), testf3) < testf4 || testf4 =
0, IF(IF(testf1< testf2 || testf2 = 0, testf1,testf2) < testf3 || testf3 =
0,IF(testf1< testf2, testf1,testf2), testf3), testf4) < testf5 || testf5 =
0,IF(IF(IF(testf1< testf2 || testf2 = 0, testf1,testf2) < testf3 || testf3 =
0,IF(testf1< testf2, testf1,testf2), testf3) < testf4 || testf4 = 0,
IF(IF(testf1< testf2 || testf2 = 0, testf1,testf2) < testf3 || testf3 =
0,IF(testf1< testf2, testf1,testf2), testf3), testf4),testf5) from TEST
where testid =1;
I didn't test that, and I really wouldn't use it. Maybe there's a prettier
way, check the function refrence for the DB you are using. I'd just pull
the results into an array and use PHP to figure it out.