MYSQL - 按日期邻近度检索结果

发布于 2024-08-28 13:42:29 字数 227 浏览 2 评论 0原文

我的问题是:

假设我们有一个带有 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 技术交流群。

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

发布评论

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

评论(3

如梦 2024-09-04 13:42:29
SELECT * FROM `calendar_table` WHERE `date_column` = (SELECT `date_column` FROM `calendar_table` ORDER BY ABS(`date_column` - UNIX_TIMESTAMP()) ASC LIMIT 1)
SELECT * FROM `calendar_table` WHERE `date_column` = (SELECT `date_column` FROM `calendar_table` ORDER BY ABS(`date_column` - UNIX_TIMESTAMP()) ASC LIMIT 1)
好倦 2024-09-04 13:42:29
SELECT *
FROM `calendar_table`
ORDER BY ABS(`date_column` - UNIX_TIMESTAMP()) DESC
LIMIT 1
SELECT *
FROM `calendar_table`
ORDER BY ABS(`date_column` - UNIX_TIMESTAMP()) DESC
LIMIT 1
揽清风入怀 2024-09-04 13:42:29

您可以做的是选择今天的日期和事件日期之间的差异,并选择最小的结果。

首先选择差异,如下所示:
从日历中选择 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.

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