mySQL SELECT 时间戳(now()-3000);
我正在尝试进行一个查询,该查询会根据时间戳(例如 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(7)
对我来说,有效的是以下查询
1800000 是 30 分钟,因为 60000 毫秒是 1 分钟
For me, what worked is following query
1800000 is 30 minutes, because 60000 ms is 1 minute
您必须对日期使用 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
首先你需要认识到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