Jump to content

Archived

This topic is now archived and is closed to further replies.

php_b34st

selecting closest number

Recommended Posts

Hi, I have a table similar to the following:
|col1|col2|
_ _ _ _ _ _
|1    |300|
_`_`_`_ _
|2    |400|
_ _ _ _ _ _
|3    |500|
_ _ _ _ _ _
|4    |600|
_ _ _ _ _ _
|5    |700|

I would like it so that when a user enters a number for col 2 I can select the corresponding number in col 1 which is closest but not higher. for example if a user inputs 450 the db would output 2 becuse 400 is closest to it without being higher. Any ideas?

Share this post


Link to post
Share on other sites
SELECT col1 from tablename WHERE col2<'$user_input_value' ORDER by col2 DESC limit 1

Share this post


Link to post
Share on other sites
wow n theres me trying to look for a complex way trying to use max() and everything. Thank you Andy

Share this post


Link to post
Share on other sites

×

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.