提取某个范围内的值

  • 本文关键字:范围内 提取 sql
  • 更新时间 :
  • 英文 :


我有下一个表

点:

min max level
0   5    1
6   10   2
11  15   3
16  20   4

我需要根据点提取水平。如果我有7分,那么等级将是2,查询是下一个:

select level
from points
where 7 > min
AND 7 < max

我需要的是获得最高水平,如果分数超过20,例如,如果我有35分,水平应该是4。我该怎么做?

将最后一行中的max设置为null:

| 16 | null  | 4

您的查询变成:

SELECT level
FROM points
WHERE :score >= min
AND (:score <= max OR max is null)

备注:最好将您的范围定义为[min,max[:

min max level
0   6    1
6   11   2
11  16   3
16       4

然后您的查询变成:

SELECT level
FROM points
WHERE :score >= min
AND (:score < max OR max is null)

最新更新