如何在 MySQL 中获得减去 6 周的时间戳?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/5091396/
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
How do I get Timestamp minus 6 weeks in MySQL?
提问by sark9012
I have a field named timestamp
. This is the last time a member was logged in.
I am looking to include a where clause in a query for something like
我有一个名为timestamp
. 这是成员最后一次登录。我希望在查询中包含一个 where 子句,例如
WHERE timestamp > todays date - 6 weeks
How would I do this? I am trying to only include users that have logged in in the last 6 weeks.
我该怎么做?我试图只包括在过去 6 周内登录的用户。
Thanks
谢谢
回答by Bryan Field
I find this syntax more readable than date_sub
, but either way works.
我发现这种语法比 更具可读性date_sub
,但无论哪种方式都有效。
WHERE timestamp >= NOW() - INTERVAL 6 WEEK
If you want to go by "Today" (midnight) instead "now" (current time), you would use this
如果你想通过“今天”(午夜)而不是“现在”(当前时间),你可以使用这个
WHERE timestamp >= DATE(NOW()) - INTERVAL 6 WEEK
回答by ajreal
where column>=date_sub(now(), interval 6 week)
回答by Neil
回答by Eric Leschinski
You can use between and now()
:
您可以在和之间使用now()
:
select somevalue
from yourtable
where yourtimestamp between now() - interval 1 day and now()
回答by rd42
Any luck yet. Have you tried:
任何运气。你有没有尝试过:
>= DATE_SUB(NOW(), INTERVAL 6 WEEK)