mySQL SELECT 时间戳(现在()-3000);

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/7604766/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-31 21:12:53  来源:igfitidea点击:

mySQL SELECT timestamp(now()-3000);

mysqltimestamp

提问by Theveloper

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

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

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.

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

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!

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

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.

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

Best regards,

此致,

John

约翰

回答by mluebke

SELECT * FROM x WHERE ts BETWEEN timestamp(DATE_SUB(NOW(), INTERVAL 30 MINUTE)) AND timestamp(NOW())

回答by ypercube??

SELECT * FROM x WHERE ts BETWEEN NOW() - INTERVAL 30 MINUTE AND NOW();

回答by Bhesh Gurung

SELECT * FROM x 
WHERE ts BETWEEN TIMESTAMPADD(MINUTE, -30, NOW()) AND NOW();

回答by Aleksandar Pavi?

For me, what worked is following query

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

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

30000 is 30 minutes, because 1000 ms is 1 minute

30000 是 30 分钟,因为 1000 毫秒是 1 分钟

回答by mugetsu

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

首先,您需要意识到 timestamp() 以秒为单位返回 unix 时间戳。3000 秒不是 30 分钟,应该是 1800 秒。试试看

回答by mugetsu

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

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

回答by musdy

SELECT * FROM (`yourdb`) WHERE `timestamp` BETWEEN NOW() - INTERVAL 30 MINUTE AND NOW();