Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/mysql/69.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何在mysql查询中使用between子句获取结果_Mysql - Fatal编程技术网

如何在mysql查询中使用between子句获取结果

如何在mysql查询中使用between子句获取结果,mysql,Mysql,我是sql新手,我需要使用中间查询从DB表中获取数据。我尝试了,但没有得到所需的结果。下面是我的表格结构 编辑: 我正在尝试按照查询中指定的期间获取利率。 如果我指定期限为6,则利率应为3,即使指定期限为11,利率也应仅为3 假设周期为13,则速率应为4 interest table ---------------------------------- id | year | min | max | interest | ---------------------------------- 1

我是sql新手,我需要使用中间查询从DB表中获取数据。我尝试了,但没有得到所需的结果。下面是我的表格结构

编辑: 我正在尝试按照查询中指定的期间获取利率。 如果我指定期限为6,则利率应为3,即使指定期限为11,利率也应仅为3

假设周期为13,则速率应为4

interest table ---------------------------------- id | year | min | max | interest | ---------------------------------- 1 | 2019 | 6 | 11 | 3 | 1 | 2019 | 12 | 0 | 4 | 但当我传递值5时,它返回null,但我仍然要求结果是“利率=3”

而且,当我通过13级考试时,我的成绩应该是“利率=4” 但我返回null

我试过,*=*即使这样也不行。
我哪里出错了?

如果您无法“修复”您的利息表,那么您可以通过计算最小值来调整它,将其重置为0,类似地,将其重置为最大最小值

所以根据你的数据

    select i.*,
        case when min = (select min(min) from interest) then 0 else min end as minmin,
        case when min = (select max(min) from interest) then 999 else max end as maxmax
from interest i

returns
+------+------+------+------+----------+--------+--------+
| id   | year | min  | max  | interest | minmin | maxmax |
+------+------+------+------+----------+--------+--------+
|    1 | 2019 |    6 |   11 |        3 |      0 |     11 |
|    1 | 2019 |   12 |    0 |        4 |     12 |    999 |
+------+------+------+------+----------+--------+--------+
2 rows in set (0.00 sec)
然后可以用来检查输入值

    select s.*
from
(
select i.*,
        case when min = (select min(min) from interest) then 0 else min end as minmin,
        case when min = (select max(min) from interest) then 999 else max end as maxmax
from interest i
) s
where 5 between minmin and maxmax and year = 2019;

+------+------+------+------+----------+--------+--------+
| id   | year | min  | max  | interest | minmin | maxmax |
+------+------+------+------+----------+--------+--------+
|    1 | 2019 |    6 |   11 |        3 |      0 |     11 |
+------+------+------+------+----------+--------+--------+
1 row in set (0.00 sec)

你的逻辑不清楚当5不在6和11之间时,为什么5会返回3?@Indrajeet evn更新后的问题无法解释你需求背后的逻辑。很明显,对于您来说,间隔是不够的,您还需要其他条件。@Indrajeet max=0是否意味着周期没有上限?如果是这样,考虑改变9999999(或SG类似的高值),并且这两个逻辑之间会起作用。<代码>但是,当我通过值5时,它返回null,但我仍然要求结果是“利率=3”。< /代码> 5低于您的最低分钟(6),因此,您确实需要解释为什么查询应该在那里返回3,而不是空的resultset。如果您在某段时间内没有产品,那么答案应该是没有可用的产品。如果有人只是忘记输入1-5期的利率,那么你不应该真的进行猜测。或者,将0存储为最小值,而不是6。
    select s.*
from
(
select i.*,
        case when min = (select min(min) from interest) then 0 else min end as minmin,
        case when min = (select max(min) from interest) then 999 else max end as maxmax
from interest i
) s
where 5 between minmin and maxmax and year = 2019;

+------+------+------+------+----------+--------+--------+
| id   | year | min  | max  | interest | minmin | maxmax |
+------+------+------+------+----------+--------+--------+
|    1 | 2019 |    6 |   11 |        3 |      0 |     11 |
+------+------+------+------+----------+--------+--------+
1 row in set (0.00 sec)