我想查询 mySQL 表以提取两个日期和两次时间之间的数据。我知道如何使用“Between”调用对单个“日期时间”列执行此操作,但我的列是一个“日期”列和一个“时间”列。我可以在网上找到的所有解决方案都是针对单个日期时间列的。
我的范围从“第 1 天”的 15:30 到第 1+1 天的 15:14
到目前为止,我可以获得以下范围(有效):
SELECT time,
close
FROM intraday_values
WHERE date="2005-03-01"
and time between "15:30" and "23:59"
但我显然需要合并 2 个日期和两次。我已尝试以下操作但收到错误:
SELECT time,
close
FROM intraday_values
between date="2005-03-01"
and time="15:30"
and date="2005-03-02"
and time = "15:14"
有人可以帮我正确地制定查询吗?非常感谢
不确定您的日期字段是否已索引。如果是这样,那么其他人给出的“concat”示例可能不会表现得很好。
作为替代方案,您可以使用以下形式的查询:
select *
from foo
where (date > lower_date and date < upper_date) -- technically this clause isn't needed if they are a day apart
or (date = lower_date and time >= lower_time)
or (date = upper_date and time <= upper_time)
它并不漂亮,但它可以工作,并且允许 mysql 使用日期字段上的索引(如果存在)。
所以你的查询是
SELECT time,
close
FROM intraday_values
where (date > "2005-03-01" and date < "2005-03-02")
or (date = "2005-03-01" and time >= "15:30")
or (date = "2005-03-02" and time <= "15:14")
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)