Jump to content


Photo

selecting closest number


  • Please log in to reply
2 replies to this topic

#1 php_b34st

php_b34st
  • Members
  • PipPipPip
  • Advanced Member
  • 168 posts

Posted 22 October 2006 - 12:48 AM

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?

#2 AndyB

AndyB
  • Staff Alumni
  • Advanced Member
  • 5,465 posts
  • LocationToronto

Posted 22 October 2006 - 12:55 AM

SELECT col1 from tablename WHERE col2<'$user_input_value' ORDER by col2 DESC limit 1
Legend has it that reading the manual never killed anyone.
My site

#3 php_b34st

php_b34st
  • Members
  • PipPipPip
  • Advanced Member
  • 168 posts

Posted 22 October 2006 - 12:58 AM

wow n theres me trying to look for a complex way trying to use max() and everything. Thank you Andy




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users