Jump to content

how to select nearest value from table


carlfaulkner
 Share

Recommended Posts

I have a table with 2 columns, width | price 100 39.95120 50.25140 63.75160 80.00 I also have an input field where the user can enter a measurement between 100 and 160 If the user enters 119 i want to run a select statement that will select the nearest row which would be 120.if the value were 105 the nearest would be 100. Or if the measurement was exact 140 would select 140 row... any ideas...

Link to comment
Share on other sites

You can use a combination of the ABS function and subtraction to get the absolute value between what they entered and what is in the columns, order the query by that result, and you can use limit if you only want to return one row (the one closest).

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

×
×
  • Create New...