我想查询一个mySQL表来提取两个日期和两个时间之间的数据。我知道如何使用“between”调用为单个“日期时间”列执行此操作,但我的列是一个“日期”列和一个“时间”列。我可以在网上找到的所有解决方案都是针对单个日期时间列的。
我的范围从 15:30 的“day1”到 15:14 的 day1+1day
到目前为止,我可以获得以下范围(有效):
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")
关于两个日期和两次之间的mySQL查询,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25076559/