简体   繁体   English

MySQL日期间隔不起作用

[英]MySQL Date Interval not working

I'm having troubles selecting future sessions were the date is 21 days from now. 我在选择将来的会议时遇到了麻烦,因为该日期是从现在开始的21天。 So not between now and 21 days but ONLY sessions that will take place 21 days from this day. 因此,不是从现在到21天,而是从今天起21天才举行的会议。

In my table dx_sessions_dates I have a field timestart of type BIGINT where a timestamp is saved (why BIGINT and not TIMESTAMP? -> Not my DB, but can't change it ... ). 在我的表dx_sessions_dates我有一个类型为BIGINT的 timestart字段,其中保存了一个时间戳记 (为什么是BIGINT而不是TIMESTAMP?->不是我的数据库,但是不能更改它...)。

My SQL Query is : 我的SQL查询是:

SELECT timestart, timefinish, sessionid FROM `dx_sessions_dates` WHERE timestart = UNIX_TIMESTAMP(DATE_ADD(NOW(), INTERVAL 21 DAY))

As you can see I want to select all the sessions where timestart is 21 days from now. 如您所见,我想选择所有时间从现在开始21天的会话。 21 days from now should be 15 april 2015 . 从现在起21天应该是2015年4月15日

The query always returns 0 rows ... . 查询总是返回0行...。 While in my table I have a timestart with value = 1429081200 . 在我的表中,我的时间开始value = 1429081200 And when you calculate the date with this you see it's 15 april 2015 . 当您以此计算日期时,您会看到是2015年4月15日 Why don't I get any rows back? 我为什么不退回任何行?

The unixtimestamp you calculate is never exactly the moment of the value you stored. 您计算出的unixtimestamp永远不会恰好是存储值的时刻。 You just need the same day. 您只需要当天。

Try 尝试

SELECT timestart, timefinish, sessionid 
FROM `dx_sessions_dates` 
WHERE date(FROM_UNIXTIME(timestart)) = curdate() + interval 21 DAY

Try using BETWEEN to find any rows that have timestart values anywhere within the 24-hour period that is 21 days from today: 尝试使用BETWEEN地发现,有24小时的时间是从今天21天的任何地方timestart值的任何行:

SELECT timestart, timefinish, sessionid 
FROM dx_sessions_dates
WHERE timestart BETWEEN UNIX_TIMESTAMP(adddate( curdate(), 21)) AND UNIX_TIMESTAMP(adddate(curdate(), 22))

curdate() returns midnight at the start of today, so 21 days from today is up to 22 days from midnight. curdate()在今天的开始返回午夜,因此从今天开始的21天最多是午夜的22天。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM