mysql在日期之间获取结果

发布于 2024-12-08 16:43:34 字数 696 浏览 0 评论 0原文

我的表中有以下行

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

萌逼全场 2024-12-15 16:43:34

假定被视为日期时间的日期具有 00:00:00 小时:分钟:秒部分。因此,2011-09-30 14:04:00 实际上是 >= 2011-09-30。

要做你想做的事,请尝试:

SELECT date_start
FROM `mytable`
WHERE date(date_start) >= "2011-09-19"
AND date(date_start) <= "2011-09-30"
LIMIT 0 , 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:

SELECT date_start
FROM `mytable`
WHERE date(date_start) >= "2011-09-19"
AND date(date_start) <= "2011-09-30"
LIMIT 0 , 30
原谅过去的我 2024-12-15 16:43:34

时间也被考虑在内。你应该这样提到

SELECT date_start
FROM `mytable`
WHERE date_start >= "2011-09-19"
AND date_start <= "2011-09-30 23:59:59"
LIMIT 0 , 30

Time is also considered. you should mention this like

SELECT date_start
FROM `mytable`
WHERE date_start >= "2011-09-19"
AND date_start <= "2011-09-30 23:59:59"
LIMIT 0 , 30
酒与心事 2024-12-15 16:43:34

2011-09-30 将被解释为 2011-09-30 00:00:00。尝试将范围的结尾设置为 2011-09-30 23:59:59 ,应该没问题。

2011-09-30 will be interpreted as 2011-09-30 00:00:00. Try setting the end of the range as 2011-09-30 23:59:59 and you should be good.

柠檬色的秋千 2024-12-15 16:43:34

就我个人而言,我会将日期和时间元素拆分为两个单独的字段:date_starttime_start,这将使您的查询能够按编码正确工作,并且还允许您做更复杂和有趣的与日期相关的活动,例如我对以下问题的回答中的解释。

选择给定日期内的所有月份跨度,包括具有 0 值的值

但是,如果您无法对系统的工作方式进行太多更改,请在您希望过滤的日期中添加一天,然后执行 << /code> 而不是<= 比较也可以。

WHERE date_start >= "2011-09-19"
    AND date_start < "2011-10-01"

这比在过滤器中执行 DATE(date_start) 更快,因为当您对可以索引的字段执行函数时,MySQL 将无法使用任何索引。

希望有帮助。

Personally, I would split the date and time elements into two separate fields, date_start and time_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.

WHERE date_start >= "2011-09-19"
    AND date_start < "2011-10-01"

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.

牵你的手,一向走下去 2024-12-15 16:43:34

date_start <= '2011-09-30' 表示包含日期 2011-09-30 00:00:00,但 2011-09-30 00:00:01 及之后的时间则不是。您可以使用 < 运算符加上常识性日期算术作为解决方法:

SELECT date_start
FROM   mytable
WHERE  date_start >= '2011-09-19'
AND    date_start <  '2011-09-30' + INTERVAL 1 DAY
LIMIT  0, 30

date_start <= '2011-09-30' implies that the date 2011-09-30 00:00:00 is included but 2011-09-30 00:00:01 and later are not. You can use the < operator plus come commonsense date arithmetic for a workaround:

SELECT date_start
FROM   mytable
WHERE  date_start >= '2011-09-19'
AND    date_start <  '2011-09-30' + INTERVAL 1 DAY
LIMIT  0, 30
梦幻的味道 2024-12-15 16:43:34

如果您在查询中使用 Between 语句,那么它将检索起始点和端点之间的所有记录。

例如:

SELECT date_start
FROM `mytable`
WHERE date_start between '2011-09-19'and '2011-09-30:23:59:59'
LIMIT 0 , 30

按照您的要求尝试一下。

if you are using between statement in query then it will retrieve all the records which are in between starting and endpoints.

For example:

SELECT date_start
FROM `mytable`
WHERE date_start between '2011-09-19'and '2011-09-30:23:59:59'
LIMIT 0 , 30

Just try this as per your requirement.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文