gpt4 book ai didi

mysql - 在 mysql 表中存储多个范围以使查询更快/更容易

转载 作者:行者123 更新时间:2023-11-30 00:39:56 27 4
gpt4 key购买 nike

我有一个具有多个时间范围的数据,例如考虑以下列

| from1 |  to1  | from2 |  to2  | from3 |  to3  |

| 06:00 | 07:30 | 09:30 | 12:30 | 13:30 | 15:45 |
| 05:00 | 06:30 | 08:15 | 14:40 | 16:30 | 18:25 |

现在,如果我想搜索一个时间,比如 08:30,我必须在查询中添加 3 个子句,以匹配输入是否出现在所有三个“从”到“到”的范围内对。

在上述情况下,它将返回第二行,因为 08:30 位于第二个“从-到”对中。

我想知道执行此操作的最佳做​​法是什么?即使我必须更改数据模型并且不将这些范围存储在如上面所示的列中,也没关系。这样我就可以快速轻松地搜索数千条记录

我想不出更好的替代方案,请提出建议。

最佳答案

我在搜索这个问题的时候发现了这个。

您的数据模型似乎未标准化。您应该考虑 morjas 关于创建附加表的建议。

下面是一个非常丑陋的查询,它检查日期是否在三个范围中的任何一个中,然后返回匹配率。

select case 
when date '2010-12-05' between range1_from and range1_to then range1_rate
when date '2010-12-05' between range2_from and range2_to then range2_rate
when date '2010-12-05' between range3_from and range3_to then range3_rate
end as rate
from events
where date '2010-12-05' between range1_from and range1_to
or date '2010-12-05' between range2_from and range2_to
or date '2010-12-05' between range3_from and range3_to;

引用。 SQL query for finding a value in multiple ranges

关于mysql - 在 mysql 表中存储多个范围以使查询更快/更容易,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21851255/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com