两个日期和两次之间的mySQL查询

s66*_*666 3 mysql sql

我想查询一个mySQL表,在两个日期和两个日期之间提取数据.我知道如何使用"之间"调用为单个"日期时间"列执行此操作,但我的列是一个"日期"列和一个"时间"列.我在网上找到的所有解决方案都是针对单个日期时间列.

我的范围从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"
Run Code Online (Sandbox Code Playgroud)

但我显然需要合并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"
Run Code Online (Sandbox Code Playgroud)

有人可以帮我正确地制定查询吗?非常感谢

Phi*_*ing 6

不确定您的日期字段是否已编入索引.如果他们是其他人给出的"连续"例子可能表现不佳.

作为替代方案,您可以使用表单的查询:

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)
Run Code Online (Sandbox Code Playgroud)

它不漂亮,但它的工作原理,并允许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")
Run Code Online (Sandbox Code Playgroud)