我有一张表
Low High Item
0 45 A
45.01 75.24 B
75.24 108.00 C
108..01 122.00 D
示例:如果我的输入为30
,则应找到它所在的范围并返回相应的项,即A
(不使用表达式中的Between或Comparison运算符)
答案 0 :(得分:2)
假设范围不重叠:
select top 1 t.*
from table t
order by (@input - low)*(high - @input) desc;
表达式(@input - low)*(high - @input)
仅对范围内的值应为正数。降序排序会将它们放在第一位。
这是使用SQL Server语法。其他数据库可能使用limit
或其他内容。
答案 1 :(得分:0)
为什么不呢
Qyery 1:
Select *
From Table1
Where 30 > Low AND 30 < High
Qyery 2:
Select *
From Table1
Where 50.02 Between Low and high
查询3:
select t.*
from table1 t
where high - 99.9 > 0 and Low - 99.99 < 0
希望这能帮到你