tags:

views:

127

answers:

1

Hi, I have SQLite database and I have in it certain column of type "double". I want to get a row that has in this column value closest to a specified one.

For example, in my table I have:

id: 1; value: 47
id: 2; value: 56
id: 3; value: 51

And I want to get a row that has its value closest to 50. So I want to receive id: 3 (value = 51).

How can I achieve this goal?

Thanks.

+6  A: 

This should work:

SELECT * FROM table
ORDER BY ABS(? - value)
LIMIT 1

Where ? represents the value you want to compare against.

Alnitak