mysql在日期之间获取结果
我的表中有以下行
date_start
-----------
2011-09-20 13:00:00
2011-10-01 13:14:00
2011-10-09 13:16:00
2011-09-27 15:00:00
2011-09-30 14:04:00
2011-10-03 14:00:00
我有这个查询:
SELECT date_start
FROM `mytable`
WHERE date_start >= "2011-09-19"
AND date_start <= "2011-09-30"
LIMIT 0 , 30
这里的问题是它只输出:
2011-09-20 13:00:00
2011-09-27 15:00:00
我期望这个结果:
2011-09-20 13:00:00
2011-09-27 15:00:00
2011-09-30 14:04:00
因为如果我们查看查询,它应该包含以下结果:2011-09-30 14:04:00
我在这里做错了什么?
我还在mysql中使用了BETWEEN,但输出仍然相同。
任何帮助将不胜感激和奖励。
谢谢!
I have this following rows in mytable
date_start
-----------
2011-09-20 13:00:00
2011-10-01 13:14:00
2011-10-09 13:16:00
2011-09-27 15:00:00
2011-09-30 14:04:00
2011-10-03 14:00:00
I have this query:
SELECT date_start
FROM `mytable`
WHERE date_start >= "2011-09-19"
AND date_start <= "2011-09-30"
LIMIT 0 , 30
The problem here is it only outputs:
2011-09-20 13:00:00
2011-09-27 15:00:00
I expect this result:
2011-09-20 13:00:00
2011-09-27 15:00:00
2011-09-30 14:04:00
Because if we look at the query it should include this as the result: 2011-09-30 14:04:00
What am I doing wrong in here?
I also used the BETWEEN
in mysql but the output is still the same.
Any help would be greatly appreciated and rewarded.
Thanks!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
假定被视为日期时间的日期具有 00:00:00 小时:分钟:秒部分。因此,2011-09-30 14:04:00 实际上是 >= 2011-09-30。
要做你想做的事,请尝试:
A date treated as a datetime is assumed to have a 00:00:00 hours:minutes:seconds part. Thus, 2011-09-30 14:04:00 is in fact >= 2011-09-30.
To do what you want, try:
时间也被考虑在内。你应该这样提到
Time is also considered. you should mention this like
2011-09-30
将被解释为2011-09-30 00:00:00
。尝试将范围的结尾设置为2011-09-30 23:59:59
,应该没问题。2011-09-30
will be interpreted as2011-09-30 00:00:00
. Try setting the end of the range as2011-09-30 23:59:59
and you should be good.就我个人而言,我会将日期和时间元素拆分为两个单独的字段:
date_start
和time_start
,这将使您的查询能够按编码正确工作,并且还允许您做更复杂和有趣的与日期相关的活动,例如我对以下问题的回答中的解释。选择给定日期内的所有月份跨度,包括具有 0 值的值
但是,如果您无法对系统的工作方式进行太多更改,请在您希望过滤的日期中添加一天,然后执行
<< /code> 而不是
<=
比较也可以。这比在过滤器中执行 DATE(date_start) 更快,因为当您对可以索引的字段执行函数时,MySQL 将无法使用任何索引。
希望有帮助。
Personally, I would split the date and time elements into two separate fields,
date_start
andtime_start
, which would then enable your query to work correctly as coded, and also allow you to do more complicated and interesting date related activities such as are explained on my answer to the following question.Select all months within given date span, including the ones with 0 values
If however you can't change much about the way the system works, adding a single day to the date you wish to filter by, and then doing a
<
instead of an<=
comparison would also work.This will work faster than performing a DATE(date_start) within your filter, as MySQL will be unable to use any indexes as you are performing a function on the fields that could be indexed.
Hope that helps.
date_start <= '2011-09-30'
表示包含日期2011-09-30 00:00:00
,但2011-09-30 00:00:01
及之后的时间则不是。您可以使用<
运算符加上常识性日期算术作为解决方法:date_start <= '2011-09-30'
implies that the date2011-09-30 00:00:00
is included but2011-09-30 00:00:01
and later are not. You can use the<
operator plus come commonsense date arithmetic for a workaround:如果您在查询中使用 Between 语句,那么它将检索起始点和端点之间的所有记录。
例如:
按照您的要求尝试一下。
if you are using between statement in query then it will retrieve all the records which are in between starting and endpoints.
For example:
Just try this as per your requirement.