MySQL 在mysql时间戳上选择日期范围
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/13418827/
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
Select date range on mysql timestamp
提问by Darren Sweeney
I am trying the following but get no results:
我正在尝试以下但没有结果:
SELECT *
FROM users_test
WHERE dateadded >= UNIX_TIMESTAMP('2012-02-01 00:00:00')
AND dateadded < UNIX_TIMESTAMP('2012-11-01 00:00:00');
Yet I know there are columns with dates within that range e.g.
但我知道有些列的日期在该范围内,例如
2012-05-11 17:10:08
Is there a better way to do this?
有一个更好的方法吗?
Eventually I want to search multiple parameters, albeit not at the same time, like today, yesterday, last week, last month etc and also a date range and month range
最终我想搜索多个参数,尽管不是同时搜索,比如今天、昨天、上周、上个月等,还有日期范围和月份范围
回答by DarkAjax
Have you tried?
你有没有尝试过?
SELECT *
FROM users_test
WHERE dateadded >= '2012-02-01 00:00:00'
AND dateadded < '2012-11-01 00:00:00'
For what I can see, it seems your table has the data stored in the same way you want to look for it (2012-05-11 17:10:08
), so in this case you won't need UNIX_TIMESTAMP.
就我所见,您的表中的数据存储方式似乎与您要查找的方式相同 ( 2012-05-11 17:10:08
),因此在这种情况下,您将不需要 UNIX_TIMESTAMP。
Also I can see you want to exclude the 2nd date from results (because you're using <
instead of <=
), otherwise using WHERE dateadded BETWEEN '2012-02-01 00:00:00' AND '2012-11-01 00:00:00'
would be fine as well...
我也可以看到您想从结果中排除第二个日期(因为您使用的是<
而不是<=
),否则使用WHERE dateadded BETWEEN '2012-02-01 00:00:00' AND '2012-11-01 00:00:00'
也可以......
回答by RTB
Just use the SQL BETWEEN
keyword. That's all.
只需使用 SQLBETWEEN
关键字。就这样。
回答by Amit Jha
try this:
尝试这个:
SELECT * FROM
users_test
WHERE
dateadded BETWEEN '2012-02-01 00:00:00' AND '2012-11-01 00:00:00'
回答by seyi_php
SELECT * FROM table_name WHERE DATE(date_field) between '2015-05-10' and '2015-05-21`