mySQL SELECT 时间戳(now()-3000);

发布于 2024-12-07 07:47:04 字数 395 浏览 1 评论 0原文

我正在尝试进行一个查询,该查询会根据时间戳(例如 30 分钟的间隔)返回结果。

所以我发现我可以

SELECT * FROM x WHERE ts BETWEEN timestamp(now()-3000) AND timestamp(now())

所以这将查询过去 30 分钟内 x 中带有 ts 列时间戳的所有内容。

然而,这只在 now() 过了 yyyy-mm-dd HH:30:00 标记之后才有效,因为在此之前的任何时候它都会导致 NULL...这是相当麻烦的,我不明白为什么它不只是从小时中减去该死的分钟!

请帮帮我!在过去 30 分钟内我找不到任何其他执行查询的方法,这就是我想要实现的目标。

最好的问候,

约翰

I'm trying to make a query which brings back results based on a timestamp, say an interval of 30 minutes.

So what I figured out is that I can

SELECT * FROM x WHERE ts BETWEEN timestamp(now()-3000) AND timestamp(now())

So this will query everything from x with timestamps in column ts within the last 30 minutes.

However, this only works after now() is past the yyyy-mm-dd HH:30:00 mark because anytime before it will result in NULL... this is rather cumbersome and I don't understand why it won't just subtract the friggin minutes from the hour!

Please help me out! I couldn't find any other method of doing a query within the last 30 minutes, that is what I'm trying to achieve.

Best regards,

John

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(7

逆夏时光 2024-12-14 07:47:05

对我来说,有效的是以下查询

select * from x where (now() - ts) < 1800000

1800000 是 30 分钟,因为 60000 毫秒是 1 分钟

For me, what worked is following query

select * from x where (now() - ts) < 1800000

1800000 is 30 minutes, because 60000 ms is 1 minute

瑾夏年华 2024-12-14 07:47:05

您必须对日期使用 DATE_ADD() 和 DATE_SUB() 运算符。查看文档: http:// dev.mysql.com/doc/refman/5.5/en/date-and-time-functions.html

You'll have to use DATE_ADD() and DATE_SUB() operators for dates. Take a look at the documentation: http://dev.mysql.com/doc/refman/5.5/en/date-and-time-functions.html

心如狂蝶 2024-12-14 07:47:05
SELECT * FROM (`yourdb`) WHERE `timestamp` BETWEEN NOW() - INTERVAL 30 MINUTE AND NOW();
SELECT * FROM (`yourdb`) WHERE `timestamp` BETWEEN NOW() - INTERVAL 30 MINUTE AND NOW();
神也荒唐 2024-12-14 07:47:04
SELECT * FROM x WHERE ts BETWEEN timestamp(DATE_SUB(NOW(), INTERVAL 30 MINUTE)) AND timestamp(NOW())
SELECT * FROM x WHERE ts BETWEEN timestamp(DATE_SUB(NOW(), INTERVAL 30 MINUTE)) AND timestamp(NOW())
猫卆 2024-12-14 07:47:04
SELECT * FROM x WHERE ts BETWEEN NOW() - INTERVAL 30 MINUTE AND NOW();
SELECT * FROM x WHERE ts BETWEEN NOW() - INTERVAL 30 MINUTE AND NOW();
千寻… 2024-12-14 07:47:04
SELECT * FROM x 
WHERE ts BETWEEN TIMESTAMPADD(MINUTE, -30, NOW()) AND NOW();
SELECT * FROM x 
WHERE ts BETWEEN TIMESTAMPADD(MINUTE, -30, NOW()) AND NOW();
流星番茄 2024-12-14 07:47:04

首先你需要认识到timestamp()返回一个以秒为单位的unix时间戳。 3000秒不是30分钟,应该是1800秒。尝试一下

First of all you need to realize that timestamp() returns a unix timestamp in seconds. 3000 seconds is not 30 minutes, it should be 1800 seconds. try that

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