offline
offline

Reputation: 69

How can i check range between all value in columns

Given bellow my MYSQL table. The value fixed:

Fuel           Percentage(%)
------         -------
150 (L)          100 
120 (L)          90 
90  (L)          70
10  (l)          20 

This data also fixed. Suppose i declare variable getValue= 130 Now i want to check the getValue=130 is which range in MYSQL table. Its easily guessable that getValue=130 range will be (120 L to 150 L) and percentage range is (90 to 100) So in this problem statement. How can i find range and find percentage of MYSQL database table ? how can i do this using python.

Upvotes: 1

Views: 91

Answers (1)

The Impaler
The Impaler

Reputation: 48850

If you are always retrieving values in a valid range (i.e. between 10 and 150 in your example) the following query gives you the range:

select
  f.fuel as fuel_from, f.percentage as percentage_from,
  t.fuel as fuel_to, t.percentage as percentage_to
from (
  select * from t where fuel <= 130 order by fuel desc limit 1
) f
cross join (
  select * from t where fuel >= 130 order by fuel limit 1
) t

Upvotes: 1

Related Questions