MYSQL - 按日期邻近度检索结果
我的问题是:
假设我们有一个带有 UNIX 日期戳 date_column 的 calendar_table,我想检索与今天日期最接近的事件。
因此,例如,如果今天没有活动,请根据日期保留最近的活动!
更新
可能我需要更清楚,我需要检索的不仅仅是一个事件,而是表中具有相同日期、最接近今天日期的所有事件!
谢谢大家! ;-)
my question is:
assuming we have a calendar_table with UNIX datestamp date_column, i want retrieve the event with the closest proximity to the today date.
So, for example if there is no event today, keep the closest one based on it's date!
UPDATED
probably i need to be more clear, i need to retrieve not just one event, but All events in the table with the same date, closest to today date!
Thanks All you guys! ;-)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您可以做的是选择今天的日期和事件日期之间的差异,并选择最小的结果。
首先选择差异,如下所示:
从日历中选择 DATEDIFF(EventDate, Now()) As Datediff
或者如果您也想查找过去的事件:
SELECT Abs(DATEDIFF(EventDate, Now())) As Datediff FROM calendar
然后只需按该字段排序并从列表中取出第一个即可。
What you can do is make select the difference between today's date and the event date, and select the smallest result.
First select the difference like this:
SELECT DATEDIFF(EventDate, Now()) As Datediff FROM calendar
or if you want to find events in the past as well:
SELECT Abs(DATEDIFF(EventDate, Now())) As Datediff FROM calendar
Then just order by that field and take the first one off the list.