Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 2 of 2
Thread: Select the next greatest value
08-24-2011, 05:30 PM #1
- Join Date
- Oct 2009
- Thanked 7 Times in 7 Posts
Select the next greatest value
Say I have the following in my database as INT values:
I wrote a query to get the Min value so it looks like this:
$qry = "Select MIN(zip_code) from zips";
Thanks in advance.
08-24-2011, 09:08 PM #2
A few different ways:
orCode:SELECT MIN(zip_code) FROM zips where zip_code NOT IN ( SELECT MIN(zip_code) FROM zips )
orCode:SELECT MIN(Z.zip_code) FROM zips AS Z, ( SELECT MIN(zip_code) AS minzip FROM zips ) AS M WHERE Z.zip_code <> M.minzip
orCode:SELECT Z.zip_code FROM zips AS Z, ( SELECT MIN(zip_code) AS minzip FROM zips ) AS M WHERE Z.zip_code <> M.minzip ORDER BY Z.zip_code LIMIT 1
Or variations on those themes.Code:SELECT M.zip_code FROM ( SELECT zip_code FROM zips ORDER BY zip_code LIMIT 2 ) AS M ORDER BY M.zip_code DESC LIMIT 1
An optimist sees the glass as half full.
A pessimist sees the glass as half empty.
A realist drinks it no matter how much there is.