mysql查询在两者之间的,两个日期两次之间的MySQL查询

I would like to query a mySQL table to pull out data between two dates and two times. I know how to do this for a single "datetime" column using the "between" call but my columns are one "date" column, and one "time" column. All the solution I can find online are for single datetime columns.

My ranges go from "day1" at 15:30 to day1+1day at 15:14

So far I can get the following range (which works):

SELECT time,

close

FROM intraday_values

WHERE date="2005-03-01"

and time between "15:30" and "23:59"

But I obviously need to incorporate 2 dates and two times. I have tried the following but get an error:

SELECT time,

close

FROM intraday_values

between date="2005-03-01"

and time="15:30"

and date="2005-03-02"

and time = "15:14"

Could someone help me formulate the query correctly? Many thanks

解决方案

Not sure if your date field is indexed. If they are then the "concat" examples others have given may not perform very well.

As an alternative you can use a query of the form:

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)

It's not pretty but it works and will allow mysql to make use of indexes on the date field if they exist.

So your query would be

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")

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值